
Code
The Elif Ladder
When there are more than two options, add elif ("else if")
branches. Python checks them top to bottom and runs only
the first one that's True:
def weather(wind):
if wind >= 60:
return "storm"
elif wind >= 20:
return "breezy"
elif wind > 0:
return "calm"
else:
return "still"
weather(30) skips the first test, because 30 isn't ≥ 60. It
passes the second test and returns "breezy". The rest are
never checked.
That's why we don't need wind >= 20 and wind < 60: if we
reached that line, we already know it's under 60.
Pip tip: Test the boundaries! For a rule like "80 or more", try 80 and 79.
Assignment
Complete oil_gauge(level), where level is a percentage:
| level | return |
|---|---|
| 80 or more | "full" |
| 30 to 79 | "half" |
| 1 to 29 | "low" |
| 0 or less | "empty" |
Complete oil_gauge(level), where level is a percentage:
| level | return |
|---|---|
| 80 or more | "full" |
| 30 to 79 | "half" |
| 1 to 29 | "low" |
| 0 or less | "empty" |
main.py Python sleeping
Press Run to try your code, then Check to test it.