Week 3, Wednesday: branching, boolean type



branching

Here's one way to write the gtw.py program from last time:

yeslist = ["y","yes","sure","Y","YES","Yes"]
nolist = ["n","no","nope","NO","No"]

if answer in yeslist:
  print("\n<large explosion>......you LOSE!\n")
elif answer in nolist:
  print("\nThe only winning move is not to play. \n\t\t\t --JOSHUA\n")
else:
  print("\nHow about a nice game of chess?\n")

branching plus accumulator

How about this one?

$ python lettercount.py 
phrase: we love computer science!!
letter: e
There are 5 e's in that phrase.

$ python lettercount.py 
phrase: we love computer science!!
letter: v
There is 1 v in that phrase.

This one requires you to go through the phrase entered by the user, on character at a time, and count up how many of a chosen letter are in the phrase.

This is a nice program, because it pulls together two things we have learned (the accumulator and branching). Make sure you get the grammar correct, too! Your program should not say "There are 1 v's in that phrase". :)

Note: don't worry about upper vs lower-case letters at this point. We will learn more python (week 6-8) that will make that easier.

boolean operators: and, or, and not

Sometimes we want to combine conditions, like this: if you start your labs early and you don't fall behind, you will do well in this class. Only if both of those conditions are True is the final result True.

There is also an or operator: if you eat lots of candy or you don't exercise, you won't be healthy. In this case, if either or both are True, the final result is True.

You can combine conditions with and, or, and not in any way you need, similar to nested code blocks. For example:

if ((condition-A) and (condition-B)) or (condition-C):
  do this code block

So if A and B are True, or C is True, the code block is executed.

Your Turn!

Write a program to 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".

$ 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
...

Note: here is where we can use the percent operator, to tell us if a number is evenly divisible by another number (or not). Here is a quick example:

>>> for i in range(20):
...   print("%d   %d" % (i, i%2))
... 
0   0
1   1
2   0
3   1
4   0
5   1
6   0
7   1
8   0
9   1
10   0
11   1
12   0
13   1
14   0
15   1
16   0
17   1
18   0
19   1

Notice, for the even numbers, i%2 is zero. We can use that in an if statement to decide what to print.

speeding tickets

Write a program to calculate speeding tickets based on the following:

Here's a quick example:

$ python ticket.py 
What is the speed limit in mph? 70
How fast were you going in mph? 80

You were speeding and your fine is $53

$ python ticket.py 
What is the speed limit in mph? 25
How fast were you going in mph? 35

You were speeding and your fine is $45