3. Conditions¶
This is the decision diamond from Unit 1 and the if / else block from Unit 2, now written with if, elif, and else:
health = 0
if health <= 0:
print("Game over.")
elif health < 2:
print("You're badly hurt.")
else:
print("You're doing fine.")
Game over.
Python has no braces {} to mark what belongs inside each branch — it uses indentation instead. Every line indented under an if/elif/else belongs to that branch; the first line back at the original indent ends it.
Try it
Write an if / elif / else that checks a variable called hour and prints a different message for "before 10pm", "10pm to midnight", and "after midnight".
Tip
A wrong indent is one of the most common Python bugs. If Python complains with IndentationError, check that every line in a branch lines up exactly.