< Prev  |  Home  |  Next >

Python ‘while’ Loops

Let’s create a game called ‘Did You Win?’. It’s exactly as amazing as it sounds:

Which looks like this:

Did you win?
...No!

Python can’t do some things (like generating a random number) without importing the necessary module, which is what this program does. The random.randint(0,1) bit generates a random integer between 0 and 1. We can then use that number to decide whether the player has won or lost. If it’s a 0, they lose, and if it’s a 1 they win. That means that the chance of winning is 50%, as is the chance of losing.

Can you change line 8 of the program above to make the chance of winning 75%. Because you’re nice like that. It makes the game a bit easier and I think a bit more playable.

This game would be even more playable if we got more than one go, and didn’t have to keep restarting the program. We could use iteration (this time a ‘for’ loop) to give the user, say, 5 goes:

I just indented all of the if/else statement and added a ‘for’ loop above it. Try out your game a few times. What do you notice? It always seems to give 5 no’s or 5 yes’s.

Did you win?
...No!
...No!
...No!
...No!
...No!

This is where I like to use print statements to test a program. Add this print line into the ‘for’ loop of your program, and then run it a few times:

You should now get the output:


Did you win?
Testing fate: 0
...No!
Testing fate: 0
...No!
Testing fate: 0
...No!
Testing fate: 0
...No!
Testing fate: 0
...No!

Whatever value you got for the fate, it never changes during each run of the program. Look carefully at the program and you’ll see why. The fate is set, and then printed 5 times. We want to set the fate and print it 5 times. We can fix the program, and remove the test print statement if it works.

If your program seems like it works, it wouldn’t hurt to test it a few times just to make sure.

We could make the game work a different way; by asking the user if they want to play again. That way, they could play once, 5 times, or 1000 times, depending on how easily amused they are.

We don’t use a for loop for that, as that would run the game a set number of times. Instead we use a while loop. Here’s a design of how we want the program to work:

Flowchart for the did you win program

Here’s the code:

This is an example of how the program should work. Notice how when the user types something other than ‘y’ when asked if they want to play again, the game ends.

Here’s a test plan:

Data Expected Actual
play == 'y' Game plays again Game plays again
play == 'Y' Game plays again Game plays again
play == 'n' Game stops Game stops

Remember that the game stops if ‘y’ (or ‘Y’) is not entered. If this is the case, what would typing ‘yes’ do? Or ‘go away’ cause to happen? Try it our if you’re not sure.


Did you win?
...No!
Play again?: y
Did you win?
...Yes!
Play again?: Y
Did you win?
...Yes!
Play again?: n

Challenge

Design, code and test a ‘double or quit’ game.

The player starts with a score of 1, and is asked whether they wish to try and double their score. If they are brave enough to take the risk, there is a 25% chance that they will end up with nothing, but a 75% change that their score will be doubled.

The game ends when the users score reaches 0, or if they choose to keep their current score and not risk another go.

The testing for this challenge can also include more general testing. You can test things like “If I am successful 3 times in a row, my score should be 8″.

< Prev  |  Home  |  Next >