0% found this document useful (0 votes)
8 views12 pages

Practical 3

Uploaded by

aad56629
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Practical 3

Uploaded by

aad56629
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

23-09-2020

Lab3 handouts
Writing a For Loop
Syntax

Range( )
Range allows us to count from one number to another while being able to define where to start
and end and how much we increment or decrement by.

Looping by Element
Continue Statement
Once a continue statement is hit, the current iteration stops and goes back to the top of the loop.

The output will result in “0, 1, 2, 4” because the continue statement is only read when num is
equal to the value of 3

Break Statemen
It allows us to break out of a loop at any point in time. The output will result in “0, 1, 2” because
we broke the loop completely when num was equal to 3.
Pass Statement
The pass statement is simply just a placeholder so that the program doesn’t break. Nothing
happens, but that’s a good thing. If you take the pass statement out completely, the program will
break because there needs to be some sort of code within the block.

Practice Program

Writing a While Loop

Nested Loops
Practice Program

Working with Lists


Checking Length

Slicing Lists
Slicing follows the same arguments as the range function start, stop, step:
Adding Items
When you need to add items to your lists, Python has two different methods for doing so.

.append( )
Append will always add the value within the parenthesis to the back of the list.

.insert( )
This method requires an index to insert a value into a specific location.

Removing Items
There are several ways to remove items from a list, the following are the main two methods.

.pop( )
By default, the pop method removes the last item in the list; however, you can specify an index
to remove as well.
.remove( )
The remove method allows us to remove items from a list based on their given value:

Working with Numerical List Data


Python provides a few functions for us to use on lists of numerical data, such as min, max, and
sum.

Sorting a List
sorted( )
.sort( )
The sort method is used for the same purpose that our previous sorted function is used for;
however, it will change the original list directly:

Conditionals and Lists


When working with lists, often you’ll need to check if values exist.

Using “in” and “not in” Keywords

Checking an Empty List


There are so many reasons to need to check for an empty list. It’s usually to ensure you don’t
cause any errors in your program, so let’s see how we can check:
Loops and Lists
Using For Loops

Using While Loops

Practice Program
Creating Hangman
Final Design
1. Select a word to play with.
2. Ask user for input.
3. Check if guess is correct.

a) If it is, show the letter in the proper place.


b) If it isn’t, lose a life.

4. Continue steps 2 and 3 until one of the following occurs:

a) The user guesses the word correctly.


b) The user loses all their lives.

This is the main game play functionality. There are several other steps we need to perform before
actually running the game, like declaring game variables; however, this is the primary
functionality that we needed to lay out before we begin coding. Knowing this structure will allow
us to stay on track with our program.

Adding Imports

Declaring Game Variables

Generating the Hidden Word


Creating the Game Loop

Outputting Game Information

Checking a Guess

Clearing Output
Creating the Losing Condition

Handling Correct Guesses

Creating a Winning Condition

Outputting Guessed Letters


Adding Guessed Letters

Handling Previous Guesses

You might also like