WEEK04: indefinite/while loops
----------------------------------------
 W: finish while loops, try/except, random library, "in" operator, object.method()

LAB3: due Saturday night

EXCEPTIONS IN PYTHON: how to handle them...

 - here's how we have been getting input from the user:

n = raw_input("Please enter an integer: ")
n = int(n)

   but this crashes if they enter something other than an integer:

Please enter an integer: q
Traceback (most recent call last):
  File "readint.py", line 3, in 
    n = int(n)
ValueError: invalid literal for int() with base 10: 'q'

 - use a try/except clause to "catch" and "handle" the ValueError exception:

try:
  n = raw_input("Please enter an integer: ")
  n = int(n)
except ValueError:
  print "that was not an integer!!"

Please enter an integer: q
that was not an integer!!

 - so now it DOESN'T CRASH...and if we put that in a loop, we can
   easily try again until we get valid input

while True:
  try:
    n = raw_input("Please enter an integer: ")
    n = int(n)
    break    # if we get an int, no need to keep looping...
  except ValueError:
    print "that was not an integer!!"
    print "please try again...."

# rest of program below...
print "You entered: %d" % (n)


Please enter an integer: q
that was not an integer!!
please try again....
Please enter an integer: 3.141
that was not an integer!!
please try again....
Please enter an integer: 7
You entered: 7

--------------------------------------------------------------

RANDOM LIBRARY: other useful functions()

  random()      returns random number between 0 and 1.0
  choice(L)     given a list (L), pick one item from the list
  shuffle(L)    shuffle the order of the items in the list L

>>> from random import *
>>> random()
0.672442656474525
>>> random()
0.8276442380360032

>>> colors = ["red","green","blue","pink","yellow"]
>>> choice(colors)
'yellow'
>>> choice(colors)
'pink'
>>> choice(colors)
'red'

>>> shuffle(colors)
>>> print colors
['pink', 'red', 'green', 'blue', 'yellow']
>>> shuffle(colors)
>>> print colors
['green', 'yellow', 'blue', 'red', 'pink']

--------------------------------------------------------------

YOUR TURN: can you write a program to simulate a coin flip?
           see how long it takes to get 100 heads!

$ python coinflip.py 

flip:   1   result: Heads   numbers of heads so far:   1
flip:   2   result: Tails   numbers of heads so far:   1
flip:   3   result: Tails   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: Heads   numbers of heads so far:   3
flip:   7   result: Heads   numbers of heads so far:   4
flip:   8   result: Heads   numbers of heads so far:   5
flip:   9   result: Heads   numbers of heads so far:   6
flip:  10   result: Tails   numbers of heads so far:   6
...
...
flip: 179   result: Tails   numbers of heads so far:  96
flip: 180   result: Heads   numbers of heads so far:  97
flip: 181   result: Tails   numbers of heads so far:  97
flip: 182   result: Heads   numbers of heads so far:  98
flip: 183   result: Heads   numbers of heads so far:  99
flip: 184   result: Heads   numbers of heads so far: 100

--------------------------------------------------------------

"in" OPERATOR tests for MEMBERSHIP

- can use it on lists and strings

>>> vowels = ['a','e','i','o','u']
>>> 'z' in vowels
False
>>> 'A' in vowels
False
>>> 'e' in vowels
True

>>> alph = "abcdefghijklmnopqrstuvwxyz"
>>> "lmnop" in alph
True
>>> "Z" in alph 
False
>>> "z" in alph
True

--------------------------------------------------------------

OBJECTS and METHODS

- in python, everything is an object (string, list, int, etc)
- objects are just data + functions tied together into one thing
- if a function is part of an object, we call it a method
- later we will learn to write/create our own objects

RADIO EXAMPLE:

 - for a real radio, you can turn it on/off, change the
   volume, change the station

 - if you wrote a program to act like a radio (like Pandora)
   you would want similar features:

     myradio = Radio()        <--- constructor creates object
                                   with initial data and methods

 - use dot operator to change the object data 
   (manipulate the object)

     myradio.setVolume(10)
     myradio.setStation(91.5)
     
     anotherradio = Radio()
     anotherradio.setStation(90.1)

     ==> call object methods to change the object

LIST and STRING methods

 - python has many useful list and string methods
 - run help(list) or help(str) to see the possibilities
 - here are some examples:

>>> L = range(15)
>>> print L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> L.reverse()
>>> print L
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> L.append(-4)
>>> L.append(99)
>>> print L
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -4, 99]
>>> L.sort()
>>> print L
[-4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 99]
>>> L.pop()
99
>>> print L
[-4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> L.index(9)
10
>>> L.index(-4)
0

NOTE: to apply a method() to an object, use the "dot" notation:

       object.method()

>>> S = "we LOVE computer science"
>>> S.upper()
'WE LOVE COMPUTER SCIENCE'
>>> S.lower()
'we love computer science'
>>> print S
we LOVE computer science

NOTE: string S is not changed!!! (strings are immutable)

>>> S.capitalize()
'We love computer science'
>>> S.count("e")
4
>>> S.split()
['we', 'LOVE', 'computer', 'science']
>>> S.index("LOVE")
3
>>> S.startswith("we")
True
>>> S.startswith("WE")
False