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:

  1. Change health to different values: 100, 60, 30, 10, 0
  2. Add a new condition for health exactly equal to 100
  3. 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 True
  • or - at least one must be True
  • not - 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:

  1. 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!
  2. Python stops checking after the first True condition

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! ๐Ÿง