Conditional Execution — Lesson 1: Make a choice with if
New skill: If statements and indentation
Previously learned: Unit 04 — comparisons and Boolean logic
Why this matters
Programs can make a choice based on a Boolean expression. Python executes the indented body of an if statement only when its condition is true.
Study this example
def warning(battery):
message = 'Ready'
if battery < 20:
message = 'Charge needed'
return message
For warning(15) the condition is true, so the message changes. For warning(90) Python skips the indented assignment and returns ‘Ready’. The colon and indentation are required.
Before moving on, predict the result of the program or function. Then run it and compare.
Check your understanding
What would warning(20) return? What does the indentation tell Python?
Common mistake
The colon after if and the indentation underneath it are part of Python syntax.
Practice
Complete classify_temperature(temperature) so it returns 'Cold alert' for temperatures below 10, and 'Normal' otherwise.
Public example 1
Input
classify_temperature(5)
Output
'Cold alert'
Public example 2
Input
classify_temperature(10)
Output
'Normal'
Public example 3
Input
classify_temperature(-4)
Output
'Cold alert'
Hint 1
Choose the comparison for below 10.Hint 2
Only update `message` when your condition is true.Hint 3
Keep `return message` after the conditional block.Reflection
Explain why your solution works and describe one boundary case you tried.