```
WEEK04: indefinite/while loops
----------------------------------------
W: more while loops...

LAB3: due Saturday night

REVIEW/SOFTWARE ENGINEERING ideas

- write out a design first!
- debug and test as you go
- use good variable names
- test, test, test (all cases!)

EXAMPLE: mathquiz that we did last week...

answer = raw_input("What is 5 x 9? ")
print "correct!"
else:

- run it and TEST it...if it works, move on to the next step...

- now generalize it to work for any two numbers (stored in variables!)

a = 5
b = 9
solution = a * b
answer = raw_input("What is %d x %d? " % (a,b))
print "correct!"
else:
print "nope...the answer was %d" % (solution)

- run it and TEST it...if it works, move on to the next step...

- now ask user for one number, use randrange() for the other

from random import randrange

a = int(raw_input("factor: "))
b = randrange(1,13)
solution = a * b
answer = raw_input("What is %d x %d? " % (a,b))
print "correct!"
else:
print "nope...the answer was %d" % (solution)

- run it and TEST it...if it works, move on to the next step...

- put all of this into a loop:

from random import randrange

a = int(raw_input("factor: "))
for i in range(5):
b = randrange(1,13)
solution = a * b
answer = raw_input("What is %d x %d? " % (a,b))
print "correct!"
else:
print "nope...the answer was %d" % (solution)

- run it and TEST it...if it works, move on to the next step...
- and so on...other things you could add:
* keep track of number correct/wrong
* output status at end (% correct, "good job", etc)

BOOLEAN FLAG variables

- here's one way to implement the "play again?" code we worked on
last time. This includes a function for playing the game, and some
code to loop until they answer y or n to the "play again?" prompt

# ------------------------------------------- #

def playgame():
"""here's our game function"""
print "-"*30
print "playing game..."
print "-"*30

# ------------------------------------------- #

def main():
"""see if user wants to play, loop to check for valid input"""

USERWANTSTOPLAY = True     # use boolean FLAG variable
# starts as True....change to False later
# if they want to quit

# ============================================ #
while USERWANTSTOPLAY:
playgame()

### loop until we get valid input ###
while True:
answer = raw_input("\nwould you like to play again? [Y/n]: ")
break
USERWANTSTOPLAY = False
break
else:
print "\nplease enter y or n!\n"
#####################################
# ============================================ #

print "\nOK...game over...bye!"

# ------------------------------------------- #

main()

- note: the outer WHILE LOOP goes until the FLAG variable (USERWANTSTOPLAY)
is set to False. The inner WHILE LOOP keeps asking until it gets valid input

YOUR TURN: add this guessing game to the "def playgame()" function above:

\$ python ggame.py
------------------------------
I'm thinking of a number from 1 to 100!
DEBUG: the num is 74
nope...too low
nope...too high
nope...too low
YOU GUESSED IT!!
------------------------------

would you like to play again? [Y/n]: zebra