if statements allow for the conditional execution of code:
Starting Out with Python refers to this as a single alternative decision structure
there is only one alternative path of execution that the code could veer off to
if-else Statements
if-else statements will execute one block of code if the condition is true… or another block if it's false:
Starting Out with Python refers to this as a dual alternative decision structure
two possible paths of execution
note that the if and else clause are part of the same structure
elif
elif Is the New Else If
We can use elif to chain together a series of conditions. This allows us to create multiple flows of execution (more than two), but - at most - only one path will be executed (even if more than one condition is true).
each condition is checked in order
if the first is false, the next condition is checked
this continues until the first true condition
the body of code associated with that condition is executed
the statement ends even if there are more conditions left
elif Syntax
Let's see what it looks like…
elif Syntax Explained
if statement like usual
go back one level of indentation to mark that the previous code block has ended
keyword elif
condition
colon
body - indented, body ends when indentation goes back one level
not required obv
even if more than one true, only the first true executes!
can still add an else at the end
A Trivial elif Example
Translate an athlete's finishing placement (1st, 2nd and 3rd) into its Olympic medal value: 1 for gold, 2 for silver, 3 for bronze and anything else means no medal →. Do this by asking for user input. For example:
Medals… Solution!
How About This?
Would this solution work? →
Nope! If you put in 1, both gold and no medal for you! are printed out
Another elif Example
Let's do this exercise using elif… →
And How Did That Compare To Consecutive If Statements?
We could have impemented this using consecutive if statements. →
An if in Your if
You can actually nest if statements within each other:
Let's See Cake With Nested If Statements
We could have impemented this using nested if statements. →
And How Did That Compare To Consecutive Nested If Statements?
What do you think the decision trees look like?. → (Oh, and BTW, what's a decision tree? …It's a graph that shows all possible decisions and the outcomes of those decisions.)
And How About Speed?
We could make an educated guess. →
elif skips conditions if one of the early conditions is true!
that means, best case, there are less instructions for the computer to execute when using elif
compared to nested ifs
or consecutive ifs
not sure what the interpreter/compiler does behind the scenes when it translates, though
could optimize things
produce similar machine code for both kinds of code
We're Not Finished Yet…
Add "maybe" as a potential answer? →
Handle different ways of saying yes (like "yeah")? →
Adding 'yeah' and 'maybe'…
Lastly, Everything Together
Write a program that names the rolls of two dice in a dice game called craps… →
print out "I don't know yet" for any other rolls. Example output:
example interaction on next page
Craps - Example Interaction
Name That Craps Roll
Nesting If Statements
we saw this above to motivate our elif example
it behaves as you'd expect
remember to get indentation right
if there are multiple elif's or else's, you can use indentation to see which initial if statement they belong to
this works for any combination of if, elif and else
note that sometimes nested if statements are equivalent to and
best to simplify - that is, less nesting, better
Nesting If Statements Example
The coffee shop has a special for half price pastries on Fridays after 4 (16:00… or 16). Ask for day and time, and make a recommendation (buy now, wait x hours or don't buy). →
Pastry Buying Guide
How to Order Conditions
if more than one condition in a series of elif's is true
only the first true condition is executed!
other are skipped, including else
be careful of conditions that never get evaluated
an above condition may already account for it
here's an example…
Ordering Conditions Continued!
The intention of the following code is to:
determine if a number is 101 or greater than 100
if it's 101, it should only print out "exactly 101" (it should not print out greater than 100)
What gets printed if n = 200? What if n = 101? →
200 → more than 100, 101 → more than 100
How to Order Conditions Continued Some More!
Of course, we could fix this. There are a few ways… →
reordering
using and
Equivalent Conditions
Logical Opposites
A way to get rid of not operators is to use the opposite logical operator:
more elegant to test intrinsic truth values than using equality operator
Let's Write a Mini Quiz Game!
Write a program to ask a couple of questions about the book, Dune. →
Let's Write a Mini Quiz Game (Continued)!
Let's get some requirements down:
ask two questions sequentially
keep track of the number of questions that the player got right
output the number of questions right
(optional) keep track of the number of questions wrong, and output that as well
(optional) ask for the player's name and greet the player
We Don't Have To Jump Right Into Code!
So, first, what's our plan? →
flow chart?
pseudocode?
Let's Write a Mini Quiz Game! (Continued Some More)!
What are some ways that we can be more tolerant about capitalization? That is… what if we wanted to accept these answers:
Arrakis / arrakis
spice / the spice / the spice melange
Another wrinkle might be to have different output based on which version of the right answer was chosen. For example, if someone puts in spice, it might say, "oh, you mean, the spice melange".