knerr cs21 notes...

back to schedule

WEEK06: indefinite (while) loops, strings as objects, random, more functions...
---------------------------------------------------------------
 M: while loops

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


REVIEW:

 - here's some code from the biggest_circle example we were
   working on (see /home/jk/inclass/biggest_circle.py). What
   does this function do? What does it return?

def findBiggest(clist):

  maxradius = clist[0].getRadius()
  biggest = clist[0]
  for c in clist:
    if c.getRadius() > maxradius:
      biggest = c
      maxradius = c.getRadius()

  return biggest


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

 - here's the syntax for a while loop:

while some_condition:
  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:

 - copy /home/jk/inclass/syracuse.py and see if you can add
   the syracuse function (if the given number is even, divide
   it by two, else multiply by 3 and add 1...and keep going
   until the value becomes 1.
    (also see http://en.wikipedia.org/wiki/Collatz_conjecture)

   here's an example run:

$ python syracuse.py 
This program generates the Syracuse sequence for an integer value
Enter an integer value: 10
syracuse function for 10:
5
16
8
4
2
1

 - copy /home/jk/inclass/fivequestions.py and see if you can
   add the getYesOrNo function. This function should take a
   question (string) as an argument, get a choice from the user
   (y or n), then return True or False. It should also keep asking
   for the y or n until the user provides valid input, like this:

Think of a number between one and five, and I'll guess it.
Is it larger than two? (y/n): r

Error...please answer y or n

Is it larger than two? (y/n): 3

Error...please answer y or n

Is it larger than two? (y/n): n
Is it two? (y/n): y
Yee ha! I guessed it was 2 in two guesses!


  *** this getYesOrNo function is a good example of a general
      function that you can use in many programs!