WEEK04: indefinite (while) loops
---------------------------------------------------------------
 M: review QUIZ 1, start while loops

REVIEW QUIZ 1

REVIEW PYTHON so far...

  types: int, float, str, bool, list
  functions: how to call them (next few weeks, how to write them)
    raw_input()     float()
    print           type()
    len()           range()
    int()           randrange()
    str()           sqrt()
  importing libraries:
    from math import *
    from random import *
  looping: for loops (while loops this week)
    for var in sequence:
      code block
  sequences we know: string, list (will use files, later)
  branching: if/elif/else
  input/output: print, print formatting, raw_input()
  algorithms: accumulator


INDEFINITE LOOPS:

 - use when you don't know how many times you need to execute
   the code in your loop. Just keep looping until some condition
   is met. Here are some examples:

  <> in a guessing game (I'm thinking of a number between 1 and 100),
     you might want to do this over an over:

        get the guess
        compare to the answer
        either output "try again" or "you've guessed it!"

  <> in a program that gives the user a menu of options, you might
     want to loop until you get a valid choice:

        print the menu of options (1=play game, 2=solve puzzle, 3=quit)
        get the user's choice
        if valid, go on with program,
        otherwise loop back to ask again

  <> in our grading program (ask for grades, print out the average)
     it might be nice to have the user just enter the grades, without
     saying ahead of time how many grades there are, and enter a
     -1 or no grade when finished

  <> in a game, like tic-tac-toe, where you don't know exactly how many 
     turns there will be (somewhere between 5 and 9)


 - here's the syntax for a while loop:

while some_condition_is_true:
  do this
  and this
  and everything indented

do this line after the while loop is done
                
 - some_condition can be anything that evaluates to True or False,
   such as i < 10, or ( i == 1 or i ==2 )

 - here's a simple example of using a while loop

x = int(raw_input("please type a number: "))

while x < 20:
  print x
  x = x + 1

print "x is now >= 20  (%d)" % (x)


 - here's an example of a "keep looping until you get a valid choice"
   program

while True:
  print_menu()
  choice = input("---> ")
  if choice==1 or choice==2:
    break
  else:
    print "that was not a valid choice"

*************************************************
DON'T CONFUSE WHILE LOOP WITH SIMPLE IF STATEMENT!!!
*************************************************


YOUR TURN: can you write this program??


$ python playagain.py 

playing game...

 would you like to play again? y
playing game...

 would you like to play again? yes
playing game...

 would you like to play again? y
playing game...

 would you like to play again? n

OK...game over...bye!

(next time we'll actually add a game!)