WEEK03: booleans, logical operators, conditionals (if/else)
-----------------------------------------------------------
 F: if/elif/elif/.../else, logical operators


REVIEW FROM LAST TIME:

/home/jk/inclass/multiply5.py

trace through this code and make sure you understand how it works!!


BRANCHING...what if we need more than two branches???

--> use elif...

$ python grade.py 
please enter your grade: 97
Your grade is: A

$ python grade.py 
please enter your grade: 76
Your grade is: C

$ python grade.py 
please enter your grade: 44
Your grade is: F


$ cat grader.py 
"""
grade program
J. Knerr -- Fall 2010
"""

def main():

  grade = input("please enter your grade: ")

  if grade >= 90:
    letter = "A"
  elif grade >= 80:
    letter = "B"
  elif grade >= 70:
    letter = "C"
  elif grade >= 60:
    letter = "D"
  else:
    letter = "F"

  print "Your grade is: %s" % (letter)

main()



LOGICAL OPERATORS: and, or, not
MEMBERSHIP OPERATOR: in

--> can combine conditions with "and" and "or"


English: if (you start labs early) and (you study for quizzes)
            ==> you will do well in CS21

** only True if both are True


English: if (you eat lots of ice cream) or (you eat lots of candy)
            ==> you will get sick

** True if either or both are True

write these out in a TRUTH TABLE and make sure you understand them!

A=start labs early
B=study for quizzes
C=do well in CS21

if A and B: C

 A     B      C
-----------------
True  True   True
False True   False
True  False  False
False False  False


>>> if speed < 70 and speed > 40:
...    print "no ticket...you can go"
... else:
...    print "you're either going too fast or two slow!"

test MEMBERSHIP with the "in" operator:

 - works on strings and lists

>>> alpha = "abcdefghijklmnopqrstuvwxyz"
>>> "a" in alpha
True
>>> "A" in alpha
False
>>> 
>>> mylist = ["pony", "horse", "zebra"]
>>> "dog" in mylist
False
>>> 


--> can you write a "do you want to play?" program to 
    handle multiple positive answers?

$ python playgame.py 
would you like to play a game? (y/n): yes
>>> SUPER. let's play tic-tac-toe...

$ python playgame.py 
would you like to play a game? (y/n): Yes
>>> SUPER. let's play tic-tac-toe...

$ python playgame.py 
would you like to play a game? (y/n): sure
>>> SUPER. let's play tic-tac-toe...


YOUR TURN: count to 100, but if the number is evenly divisible
by 3, say bizz instead of the number. If the number is evenly 
divisible by 5, say buzz, and if evenly divisible by 3 and 5, say
bizzbuzz.

--> can you write the bizzbuzz game??

$ python bizzbuzz.py 
1
2
bizz (3)
4
buzz (5)
bizz (6)
7
8
bizz (9)
buzz (10)
11
bizz (12)
13
14
bizzbuzz (15)
16
17
bizz (18)
...
...
89
bizzbuzz (90)
91
92
bizz (93)
94
buzz (95)
bizz (96)
97
98
bizz (99)
buzz (100)