LEVEL 5
Programs that decide
if this is true, do that. Otherwise, do something else.
press Enter ↵
🐍 main.py
Ctrl + Enter runs it too. Esc, then Tab, leaves the code.
Output

Press ▶ Run to see what your program does.

Lesson 34: Decisions: if and else

Level 5, Python Thinks and Repeats. Umbrella or sunglasses? Let the program decide. The new word: if. It follows lesson 33, Level 4 test: Python Starter. Next comes lesson 35, Comparisons and indents.

What the lesson explains

Programs that decide. if this is true, do that. Otherwise, do something else.

Umbrella or sunglasses?. The lines under if run only when the test is True. On the card: if weather == "rain":

What you do first

Change the weather. Make it print Take sunglasses. The editor opens with: weather = "rain" / if weather == "rain": / print("Take an umbrella") / else: / print("Take sunglasses"). One way to write it: weather = "sun" / if weather == "rain": / print("Take an umbrella") / else: / print("Take sunglasses"). It prints: Take sunglasses.

The code on the screen

Umbrella or sunglasses?. The editor shows 5 lines: weather = "rain" / if weather == "rain": / print("Take an umbrella") / else: / print("Take sunglasses"). Run it, and it prints "Take an umbrella".

What you practise

Try it, "Change the weather": Make it print Take sunglasses. The editor opens with: weather = "rain" / if weather == "rain": / print("Take an umbrella") / else: / print("Take sunglasses"). One way to write it: weather = "sun" / if weather == "rain": / print("Take an umbrella") / else: / print("Take sunglasses"). It prints: Take sunglasses.

Try it, "Your own decision": Print You win! for a score of 12. The editor opens with: score = 12. One way to write it: score = 12 / if score >= 10: / print("You win!") / else: / print("Keep going"). It prints: You win!.

Fix it, "The missing colon": Fix the SyntaxError. The editor opens with: age = 9 / if age < 10 / print("Kid ticket"). One way to write it: age = 9 / if age < 10: / print("Kid ticket"). It prints: Kid ticket.

What PlayBuddy and PlayType say

PlayType: "Welcome to Level 5! Now your programs can make decisions."

PlayBuddy: "After the test comes a colon. The next lines move in four spaces."

Help in every lesson

Everything happens on a pretend computer in your browser, so nothing touches your real files. PlayBuddy and PlayType stand beside the board, cheer you on and say when it is your turn.

A green ring shows where to click the first time. After that you do it on your own, and if you slip twice the ring comes back.

Stars

The lesson has 3 things to do. Up to one slip in ten gives three stars, up to one in four gives two, and finishing always gives at least one. Stars and progress are saved in this browser only; there is no account.

About DayOneCode

DayOneCode is an independent learning site, not affiliated with or endorsed by Anthropic. Claude and Claude Code are trademarks of Anthropic.