Quest 3: Conditionals - Choose Your Path
Make Decisions in Your Code with If/Else
๐ค๏ธ QUEST 3 | Difficulty: Beginner | Time: 5 minutes
๐ Complexity Level: Beginner โญ
Perfect for students with no programming experience. This lesson introduces fundamental concepts that are essential for all programming. Great for college fair visitors and first-time coders!
๐ป Interactive Options:
- ๐ Open in JupyterLite - Full Jupyter environment in your browser
- โถ๏ธ Run code directly below - All code cells on this page are editable and runnable
- ๐ฅ Download Notebook - For use in local Jupyter or Google Colab
๐ Introduction: The Fork in the Road
Youโre walking through an enchanted forest when you come to a fork in the road. A sign says: - Left path: โIf you have a key, go this way to the treasureโ - Right path: โIf you donโt have a key, go this way to find oneโ
Your choice depends on a condition: Do you have a key or not? This kind of decision-making is exactly what conditionals do in programming!
๐ณ Story Time: Every adventure is full of choices. Should you fight the dragon or sneak around it? That depends on your health. Should you buy the magic sword? That depends on your gold. Conditionals help your program make these smart decisions!
๐ก Explanation: If Statements
A conditional statement lets your program choose different paths based on whether something is true or false.
Basic If Statement
if condition:
# Do this if condition is True
print("Condition was true!")If-Else Statement
if condition:
# Do this if condition is True
print("Yes!")
else:
# Do this if condition is False
print("No!")If-Elif-Else Statement
if condition1:
# Do this if condition1 is True
print("First choice!")
elif condition2:
# Do this if condition2 is True
print("Second choice!")
else:
# Do this if all conditions are False
print("Default choice!")๐ Comparison Operators:
Use these to create conditions:
==equal to!=not equal to>greater than<less than>=greater than or equal to<=less than or equal to
Examples:
health > 50 # Is health more than 50?
age == 15 # Is age exactly 15?
name != "Guest" # Is name not equal to "Guest"?๐ฎ Activity: The Health Check
Run this code to see conditionals in action. Try changing the health value!
๐ฏ Challenge: Modify the code to check different health thresholds:
- Change health to different values: 100, 60, 30, 10, 0
- Add a new condition for health exactly equal to 100
- What happens if health is 50? Which message appears?
๐จโ๐ป Code Example: The Treasure Room
Letโs build a more complex decision system:
๐ก Logical Operators:
Combine multiple conditions:
and- both must be Trueor- at least one must be Truenot- reverses True/False
Examples:
if health > 50 and has_weapon:
print("Ready to fight!")
if is_daytime or has_torch:
print("You can see!")๐งฉ Puzzle Time!
What will this code print? Think carefully, then run it:
๐ Solution Explained:
The code will print: โPath Aโ
Why?
Letโs trace through the conditions:
- First check:
score >= 80 and has_completed_tutorial- score is 85, which is >= 80 โ
- has_completed_tutorial is True โ
- Both conditions are True, so this branch executes!
- Python stops checking after the first
Truecondition
Important: Even though the second condition (score >= 80) is also true, Python never checks it because the first condition already matched. This is called โshort-circuit evaluation.โ
Experiment: Try these combinations: - score = 85, has_completed_tutorial = False โ Path B - score = 70, has_completed_tutorial = True โ Path C - score = 70, has_completed_tutorial = False โ Path D
๐ฏ Key Takeaways
โจ Quest 3 Complete! โจ
Youโve learned:
โ
Use if to execute code when a condition is True
โ
Use else for when the condition is False
โ
Use elif to check multiple conditions
โ
Comparison operators: ==, !=, >, <, >=, <=
โ
Logical operators: and, or, not
โ
Python checks conditions in order and stops at the first True
Next Quest: Ready to repeat actions? Try Quest 4: Loops!
๐ Try This at Home!
Create a simple game choice system:
# Create your own adventure!
player_choice = "forest" # Try: "forest", "cave", "mountain"
if player_choice == "forest":
print("๐ฒ You enter the dark forest...")
print("A wild wolf appears!")
elif player_choice == "cave":
print("๐ฆ You enter the mysterious cave...")
print("You find ancient treasure!")
elif player_choice == "mountain":
print("โฐ๏ธ You climb the tall mountain...")
print("You meet a wise wizard!")
else:
print("โ That's not a valid choice!")
# Make it more complex by adding more choices and conditions!๐ฑ Great Progress! You can now make your programs think and decide. Thatโs powerful! ๐ง