Date

Lecture Date: Friday, September 16

Programs that start and then end without any choices to make are boring. If we are going to write a program to do anything interesting, it has to be able to make decisions based on the data it is seeing.

Enter the if statement.

Consider this situation:

ifstatement.png

Pretty straight forward. But how do we make the computer do something different based on the input?

First, how do we decide whether it is cold outside or not?

Well, we could ask the user:

temp = input("What is the temperature outside?: ")

This is good, but this doesn't yet tell us whether it is specifically cold or not. And that's different for everyone. So let's add this:

cold_threshold = 30

So, now let's test whether it is cold or not!

if temp <= cold_threshold: 
    wear_coat()
    wear_hat()
    wear_gloves()

There's another way we could do this - and that's with what's called a flag variable. A flag variable is a bool that we use to determine some sort of state. In this case, we would write it like this:

is_cold = False

if temp <= cold_threshold:
    is_cold = True

if is_cold:
    wear_coat()
    wear_hat()
    wear_gloves()

This makes the code a bit more readable, and is_cold can be easily reused later on.

Now, what if we had to make more of a decision here:

ifelsestatement.png

We could use our flag variable again, but now we will add another option to our if statement:

is_cold = False;

if temp <= cold_threshold:
    is_cold = True

if is_cold:
    wear_coat()

else:
    wear_shorts()

In this case, we must do one or the other. A simple if statement either will or will not execute. Here, if the if is not executed, then the else will be.

We can string more of these together as well!

if is_cold:
    wear_coat()

elif today_is_sunday:
    wear_blazer()

else:
    wear_shorts()

Now you are guaranteed to execute one of these three paths.

Let's improve our translation program from before. Here's where we left it:

dictionary = {"hello":"Yarrr!!"}

word = input("What word would you like translated?: ")
print(word + " translates to " + dictionary.get(word.lower(), 'N/A') + " in pirate speak")

Other points for today:

  • How to indent your code properly
  • Relational operators (<, >, ==, >=, <=, !=)
  • Nested if statements
  • How to test for your letter grade in the class
  • How to deal splitting an input
  • Adding to a dictionary

More code from today:

cold_threshold = int(input("What is your cold threshold? "))
current_temp = int(input("What is the current temp? "))
is_cold = False
pants_day = False

if current_temp < cold_threshold:
   is_cold = True

if is_cold and pants_day:
   print("Wear coat")
   print("Wear scarf")
   print("Wear pants")

if is_cold:
   print("Wear more pants")
else:
   print("Who needs pants?")

if is_cold:
   print("Wear more pants")
elif pants_day:
   print("Must wear pants on pants day!")
elif True:
   print("Yup, that's 1 == 1...")
else:
   print("Who needs pants?")

# < >
# <= >=
# ==
# not

Pirate Translator:

# 1. Ask the user for a word to translate
word = input("Word to translate: ")
# 2. Look up that word in a dictionary of words
pirate_dictionary = {"hello" : "yarr", "hi" : "ahoy!"}
# 3. Print the translation OR print that it does not exist

word_to_add = input("Give me a word and translation to add: ").split()
# if word exists, tell user it is already there
if word_to_add[0] in pirate_dictionary:
   print("That is already here")
else:
   pirate_dictionary[word_to_add[0]] = word_to_add[1]
# if word does not exist, add the word and translation

if word.lower() in pirate_dictionary:
   print("The translation is:", pirate_dictionary.get(word.lower(), "Not here!"))
else:
   print("I cannot translate that...")