WEEK06: indefinite (while) loops
---------------------------------------------------------------
 M: stack diagram, while loops

ANNOUNCEMENTS:
 - quiz this Friday...functions!
 - Lab 5 due tomorrow night (questions??)


FUNCTIONS and LISTS:

 - see file /home/jk/inclass/functionsandlists.py
 - see file /home/jk/inclass/zmax.py

 - make sure you understand how the zmax.py program works!
 - try running it through the www.pythontutor.com website


"""
zero-out max of a list.
program to show how lists can be changed in functions.

try it in www.pythontutor.com

J. Knerr
Fall 2012
"""

from random import randrange

#######################################################

def main():
  """set up list, send to function"""

  # set up random list
  L = []
  N = 10
  for i in range(N):
    value = randrange(1,101)
    L.append(value)

  print "BEFORE:"
  print L

  zeroMax(L)

  print "AFTER zeroMax() call:"
  print L

#######################################################

def zeroMax(mylist):
  """find largest item in list, set it to zero"""

  index_of_max = 0
  for i in range(len(mylist)):
    if mylist[i] > mylist[index_of_max]:
      index_of_max = i

  # now that we have index of max item in list, 
  # use it to put a zero there
  mylist[index_of_max] = 0

  return  

#######################################################
main()

 - see pages 184-187 if you don't understand why the list L in
   main() is changed
 - the function that is currently executing is on top of the stack
 - a stack is a Last-In-First-Out (LIFO) data structure


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 an example of using a while loop to mimic a for loop
   (not a great use of the while loop, but just to see how it works...)

i = 0
while i < 10:
  print i
  i = i + 1

 - 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"


YOUR TURN:

 - write a simple guessing game program:

$ python ggame.py 

I'm thinking of a number from 1 to 10...

guess: 3
nope...try again.
guess: 5
nope...try again.
guess: 6
nope...try again.
guess: 4
You got it!
G A M E  O V E R