🐸

The Choice

Python Basicspython-basics-10-the-choice
Reward: 80 XP
|

The Choice

The golem steps aside, revealing a magical door. "If you have the key," it says, "the door will open."

In Python, we use the if statement to make decisions.

The If Spell

has_key = True

if has_key:
  print("The door opens!")
  print("Hoppy walks through.")

The code inside the if block only runs if the condition is True.

The Grammar of Magic

In Python, two symbols are crucial here:
1
The Colon (:)

Like a gate, the if line MUST end with a colon. It tells Python a new block is starting.

2
Indentation

Like steps after the gate, the code inside must be shifted right (usually 4 spaces).

Your Mission

1
The Condition

Create a variable is_hungry and set it to True.

2
The Decision

Write an if statement that checks is_hungry.

3
The Action

Inside the if block, print "Eat a magical berry".

4
Afterwards

Outside the if block (no indentation), print "Continue journey".

Indentation Matters

Python uses indentation (spaces at the start of the line) to know what code belongs inside the if. Standard practice is 4 spaces.

Suggested Solution
Click to expand

Here is how to make a decision:

is_hungry = True

if is_hungry:
  print("Eat a magical berry")

print("Continue journey")
Advanced Tips
Want more? Click to expand

You can put anything that is True or False in the condition, even a comparison!

energy = 80
if energy > 50:
  print("Ready to run!")
pymain.py
Loading...
Terminal
Terminal
Ready to run...