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
 - use good comments
 - test, test, test (all cases!)

EXAMPLE: mathquiz that we did last week...

 - start with the simplest case:

answer = raw_input("What is 5 x 9? ")
answer = int(answer)
if answer == 45:
  print "correct!"
else:
  print "nope...the answer was 45"

 - 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))
answer = int(answer)
if answer == solution:
  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))
answer = int(answer)
if answer == solution:
  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))
	answer = int(answer)
	if answer == solution:
		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]: ")
      if answer == "" or answer == "y" or answer == "Y" or answer == "yes":
        break
      elif answer == "n" or answer == "N" or answer == "no":
        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
your guess: 50
nope...too low
your guess: 75
nope...too high
your guess: 70
nope...too low
your guess: 74
YOU GUESSED IT!!
------------------------------

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

please enter y or n!


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

OK...game over...bye!