If Operations
Checkpoint #1
Whitespace
Elif
Checkpoint #2
Nested If
Summary
Elif
Sometimes it is better to have more than two conditions so that we can capture more possibilities. To do this, we can use another conditional command called "elif" (short for "else if"), which evaluates only if the preceding if statement is not true. This allows us to make more complicated flowcharts of commands. Take a look at the example below
1 tempInCelsius = 25
2 if tempInCelsius < 0:
3 print("Your water is frozen")
4 elif tempInCelsius > 100:
5 print("Your water is boiling")
6 else tempInCelsius < 0:
7 print("Your water is liquid (not freezing or boiling)!")
Can you predict what the output of the following code would be?
Let's try another example. We have written a program that should print out a password depending on the size of the number. Try to select a "secretID" that would cause the program to print: "Your Pokemon is: MewTwo"
1 secretID = #type in the number here
2 if secretID > 100:
3 print("Your Pokemon is: Charizard!")
4 elif secretID > 70:
5 print("Your Pokemon is: Pikachu!")
6 elif secretID > 40:
7 print("Your Pokemon is: Rattata!")
8 elif secretID => 40:
9 print("Your Pokemon is: MewTwo!")
10 else:
11 print("Your Pokemon is: Wobbuffet!")