π Story: Zogg Needs a Fuel Tracker! β½π
Zogg is getting closer to fixing his spaceship, but there’s a new problem!
βOh no! I keep forgetting how much fuel I have left! Writing the same numbers over and over is so confusing! Is there a way to make Python remember things for me?β
Great question, Zogg! Luckily, Python has a special tool to store informationβthey’re called variables! Today, weβll learn how to use them to help Zogg keep track of his spaceshipβs fuel.
π§ What Are Variables?
A variable is like a magic box where we can store numbers, words, or other information. Instead of writing the same number over and over, we save it in a variable and use it whenever we need!
For example:
fuel = 1000
print(fuel)
When we run this code, Python will show:
1000
π Python remembers the number for us!
π Step 1: Open Your Python Playground
Use one of these free online compilers (no sign-up needed!):
πΉ Trinket – Python Online (Recommended β
)
πΉ OnlineGDB – Python Compiler
πΉ Programiz – Python Online Compiler
π Step 2: Create Your Own Variables!
Letβs help Zogg keep track of his spaceshipβs fuel! Try this code:
fuel = 500
print(fuel)
What do you see? Python remembers that Zogg has 500 units of fuel!
Now try changing the number inside fuel
and run the code again!
π§ͺ Step 3: Variables + Math!
Since variables store numbers, we can do math with them! Try this:
fuel = 500
used_fuel = 200
fuel_left = fuel - used_fuel
print(fuel_left)
What happens? Python subtracts 200
from 500
and prints the answer:
300
π Now Zogg knows how much fuel is left!
π‘ Why Are Variables Useful?
Imagine if Zoggβs spaceship had 10 fuel tanks, each holding 500
units of fuel. Instead of writing 500
ten times, we can use variables!
fuel_per_tank = 500
total_tanks = 10
total_fuel = fuel_per_tank * total_tanks
print(total_fuel)
Python will show:
5000
Thatβs a lot of fuel! ππ₯
π― Challenge: Track Zoggβs Journey!
Zogg starts with 1000
fuel units. Every hour, his spaceship uses 150 units of fuel.
π Mission: Write a Python program to calculate how much fuel is left after 3 hours!
Use this template:
fuel = 1000
fuel_used_per_hour = 150
fuel_left = fuel - (___ * ___) # Fill in the blanks!
print(fuel_left)
Post your answer in the comments! π
β οΈ Common Mistakes and Fixes!
β Using spaces in variable names
fuel per tank = 500 # β ERROR!
β Fix:
fuel_per_tank = 500 # β
Works!
β Forgetting quotation marks for text
name = Zogg # β ERROR!
β Fix:
name = "Zogg" # β
Works!
π Fun Fact: Pythonβs Name Rules for Variables!
Python has some rules for naming variables:
β
Can use: letters (a-z
), numbers (0-9
), and _
(underscore)
β Canβt start with a number! (2fuel = 500
β)
β No spaces! (fuel per tank = 500
β)
π Whatβs Next?
Zogg can now track his spaceshipβs fuel! But thereβs one more problemβ¦ He wants to ask the user for input instead of changing the code every time!
π Next Mission: “Talking to Python: User Input!” π€π¬
Comment below:
1οΈβ£ What was your answer to Zoggβs fuel problem?
2οΈβ£ Did you enjoy this lesson?
See you in the next adventure, Junior Python Coder! π§βππ©βπ