The dragon explains
if, by itself
The dragon: Now you can ask a question. Let's actually DO something based on the answer.
The dragon: if CONDITION: means 'the next line only runs when this is True'.
example.pyPython 3health = 20if health < 30: print("Watch out!")
OutputWatch out!The dragon: See the indent? Everything indented under the if belongs to it — that's how Python knows what's conditional.
example.pyPython 3if health < 30: print("Watch out!")
The dragon: And here's the important part: if the condition is False, absolutely nothing happens. That's normal.
example.pyPython 3health = 90if health < 30: print("Watch out!")
The dragon: No else needed. An if all by itself, doing nothing when it's False, is completely correct.
The dragon: Now it gets real: your code will run in several caves at once. A knight in one, a sheep in another. Only an if can handle both.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 1
health = 90. What happens here?
question.pyPython 3health = 90if health < 30: print("Watch out!")print("Done checking.")
- Just: Done checking.Correct
- This causes an error — an if needs an else
- Watch out! then Done checking.
Why: health is 90, so health < 30 is False. The indented print never runs, and Python moves straight to the next line: Done checking. No else, no error — this is exactly how if is meant to work alone.
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
Your code runs in TWO caves. In one, a knight blocks the path — breathe fire and he runs. In the other, a sheep is just grazing, and scaring it would be mean. This code always breathes fire. Put it inside an if, so the dragon only does it when sees_knight() is True.
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.