WEEK06: indefinite (while) loops, more functions... --------------------------------------------------------------- W: more while loops, functions ANNOUNCEMENTS: - quiz this Friday...functions! - lab 6 (typing tutor) due THURSDAY after break REVIEW: - main loop from guessing game: num = randrange(1,11) user_guess = input("--> ") while num != user_guess: print "nope...try again." user_guess = input("--> ") print "G A M E O V E R\n" - and here's another way to write that, using while TRUE construct: num = randrange(1,11) while True: user_guess = input("--> ") if user_guess == num: print "You got it!" break else: print "nope...try again." print "G A M E O V E R\n" more WHILE LOOPS practice: - can you write this program to simulate flipping a coin until we get 100 HEADS? $ python coinflip.py I'm going to flip a coin over and over. I'll stop after I get 100 heads. Hit return to begin... flip: 1 result: Tails numbers of heads so far: 0 flip: 2 result: Tails numbers of heads so far: 0 flip: 3 result: Heads numbers of heads so far: 1 flip: 4 result: Tails numbers of heads so far: 1 flip: 5 result: Heads numbers of heads so far: 2 flip: 6 result: Tails numbers of heads so far: 2 flip: 7 result: Heads numbers of heads so far: 3 flip: 8 result: Heads numbers of heads so far: 4 flip: 9 result: Heads numbers of heads so far: 5 flip: 10 result: Heads numbers of heads so far: 6 ... and so on... ... flip: 208 result: Heads numbers of heads so far: 97 flip: 209 result: Heads numbers of heads so far: 98 flip: 210 result: Heads numbers of heads so far: 99 flip: 211 result: Tails numbers of heads so far: 99 flip: 212 result: Heads numbers of heads so far: 100 ANOTHER ONE: - imagine we have a program that uses lots of menus (like the adventure game). Sometimes the choices are from 1-3, other times they might be 1-4 or more: What would you like to do?? 1. Start your CS homework 2. Go to the library 3. Poke the dragon ---> 0 Error..please enter an int from 1 to 3!! - it would be nice to have a general function that gets an integer from the user and checks to make sure it is valid. If it *is* valid, return the integer. If not, give the user another chance (hint: use a WHILE loop!) - write a function, getIntInRange, that gets *and* checks an input from the user. If the user enters an integer that is in the correct range (like 1-3 below), return the integer. If the user enters an invalid integer, tell them and ask again. $ python getIntInRange.py What would you like to do?? 1. Start your CS homework 2. Go to the library 3. Poke the dragon ---> 0 Error..please enter an int from 1 to 3!! ---> 44 Error..please enter an int from 1 to 3!! ---> 3 Your hair is now on fire. Here's an example of how you might call this function from a main program: print menu choice = getIntInRange(1,3) if choice == 1: print "\nSuper!! You get an A+!!\n" elif choice == 2: print "\nOK...have fun.\n" elif choice == 3: print "\nYour hair is now on fire.\n" So getIntInRange() does the getting and the checking, and only returns when it has received valid input from the user. BONUS: make your getIntInRange() function work (not crash) no matter what the user enters... $ python adventureWithFunctions.py You are in a dimly lit computer room. A lab that is worth 50% of your grade is due in 4 hours. What do you want to do? 1 Start work on your lab 2 Go play ultimate frisbee with your friends 3 Take a nap on the CS couches ---> hello # strings don't crash it please enter an integer from 1 to 3 ---> -4 please enter an integer from 1 to 3 ---> 1 -=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=- An evil ninja offers you all of the solutions to the lab that's due in 3 hours. What do you want to do? 1 Take the solutions and thank her 2 Ignore her and keep working on your CS homework 3 Take a nap on the CS couches 4 Go watch videos on the internet ---> 2 # even leading spaces don't cause problems Congratulations!! You finish your homework on time and get an A in the class.