π Story: Zoggβs Fuel Crisis! β οΈπ
Zogg is doing great! He can now enter fuel amounts and track how much is left. But suddenlyβ¦
BEEP BEEP! An alarm goes off inside the spaceship. π¨
βOh no! My fuel is running low! But how do I know if I have enough to reach my home planet?β
Zogg needs Python to think and make decisions for him! Thatβs where if-else statements come in. Today, weβll teach Python how to make smart choices. π€
π€ What Are If-Else Statements?
If-Else statements help Python decide what to do based on conditions.
For example:
fuel = 300
if fuel > 500:
print("You have enough fuel! π")
else:
print("Warning! Fuel is low! β οΈ")
πΉ If the fuel is more than 500, Python says βYou have enough fuel!β πΉ If the fuel is 500 or less, Python gives a warning!
This is called conditional logic, and itβs super powerful! π§ β¨
π Step 1: Letβs Warn Zogg About Low Fuel!
Try this code in your Python playground:
fuel = int(input("How much fuel do you have? "))
if fuel > 500:
print("Zogg, you have enough fuel! π")
else:
print("Zogg, warning! Fuel is running low! β οΈ")
Try entering different fuel amounts. What happens when you enter 600? What about 200?
π Step 2: Adding More Conditions!
What if we want to give different warnings for different fuel levels?
fuel = int(input("How much fuel do you have? "))
if fuel > 500:
print("Zogg, you have plenty of fuel! π")
elif fuel > 200:
print("Zogg, your fuel is getting low! Be careful! β οΈ")
else:
print("Zogg, emergency! You need more fuel now! π₯")
πΉ elif (short for βelse ifβ) lets us check more than one condition! πΉ Now, if Zogg has between 201-500 fuel, he gets a caution warning. πΉ If he has 200 or less, he gets an emergency alert!
Try testing different numbers. Does the message change correctly?
π― Challenge: Zoggβs Safe Landing!
Zogg needs at least 300 fuel to land safely on his home planet.
π Mission: Complete this code to tell Zogg if he can land safely!
fuel = int(input("Enter fuel amount: "))
if ____: # Fill in the condition!
print("Zogg, you can land safely! π‘β¨")
else:
print("Zogg, you need more fuel to land! π₯")
Can you complete it? Post your answer in the comments! π
β οΈ Common Mistakes and Fixes!
β Using = instead of == for conditions
if fuel = 500: # β ERROR!
β Fix:
if fuel == 500: # β
Works!
β Forgetting to indent (Python needs spaces!)
if fuel > 500:
print("You have enough fuel!") # β ERROR!
β Fix:
if fuel > 500:
print("You have enough fuel!") # β
Works!
π Fun Fact: Computers Think in True or False!
If-Else statements work because Python checks if something is True or False. Try this:
print(10 > 5) # True
print(2 < 1) # False
Computers LOVE logic! π€β€οΈ
π Whatβs Next?
Now that Zogg can make smart decisions, what if he wants to repeat tasks automatically? Next time, weβll learn about loops to make Python even more powerful! πβ¨
π Next Mission: βLoops – The Power of Repeating!β ππ
Comment below: 1οΈβ£ What message did you get when you tested different fuel levels? 2οΈβ£ Did you enjoy this lesson?
See you in the next adventure, Junior Python Coder! π¦π