Chapter 2: The Magic Inventory
Ten-year-old Leo spent three days building the ultimate pizza restaurant in Roblox, but there was one huge problem. Every time a player bought a slice of pepperoni, the game completely forgot to give them their points! Leo realized that without a way for the game to remember things, his awesome pizza parlor was totally broken.
In the last chapter, you learned how to give the computer orders using print. But right now, your computer has a short memory. It does exactly what you say, then forgets everything immediately! To make a real game, the computer needs to remember things.
In Python, we use something called a Variable Variable: A labeled container or 'box' that stores information for the computer to use later. . A variable is like a labeled box where the computer stores information to use later.
Creating a Variable
Section titled “Creating a Variable”Creating a variable is like labeling a chest and putting something inside it. We need two things:
- The Name: What is on the label? (e.g.,
score,gold,player_name) - The Value: What is inside the box? (e.g.,
100,"Steve")
In Python, we use the assignment operator assignment operator: The symbol used to put a value into a variable (it means 'is set to', not 'equals'). (the equals sign =) to put things in the box.
Minecraft Example:
blocks = 64- The Label:
blocks - The Value:
64 - The Translation: “Computer, create a box named ‘blocks’ and put the number 64 inside it.”
Roblox Example:
run_speed = 16- The Label:
run_speed - The Value:
16
Real-World Example: Think about your cubby or a locker at school. The label on the outside has your name on it, and the “value” inside is your backpack, your jacket, and maybe a squished sandwich. You always know exactly where to find your stuff because of that label!
Using Your Variables
Section titled “Using Your Variables”Once you have stored something, you can use it! You don’t need to type the number 64 over and over again. You just use the variable name.
Let’s try a script to introduce a player. Type this code:
player_name = "NoobGamer123"score = 0
print("Welcome,")print(player_name)print("Your current score is:")print(score)What happens:
The computer looks inside the variable player_name, finds "NoobGamer123", and prints it. Then it looks inside score, finds 0, and prints that!
Combining Words and Variables
Section titled “Combining Words and Variables”Printing on separate lines is okay, but it looks a bit robotic. Let’s make it look like a real game message by putting words and variables on the same line.
To do this in Python, we use a comma ,. The comma acts like glue—it sticks your message and your variable together.
Try this:
my_pet = "Dragon"coins = 50
print("You equipped your", my_pet)print("You have", coins, "coins")Output:
You equipped your DragonYou have 50 coinsNotice how Python automatically adds a little space where the comma was? It keeps your sentences neat!
Real-World Example: Imagine writing a birthday card where you fill in the blanks. “Happy Birthday, [Name]! You are turning [Age] today!” The words on the card stay exactly the same, but the blanks are like variables. You can glue in “Sarah” and “10” or “Tommy” and “9”, and the sentence still makes perfect sense.
Updating Variables (Looting and Leveling Up!)
Section titled “Updating Variables (Looting and Leveling Up!)”Variables are powerful because they can change. In fact, the word “variable” actually means “able to vary” (change).
We can change a variable by doing a little bit of math.
The Math of Gaming: Let’s say you found some diamonds.
diamonds = 3print("Diamonds found:", diamonds)
# You mined 2 more!diamonds = diamonds + 2print("Total diamonds:", diamonds)Output:
Diamonds found: 3Total diamonds: 5When you type diamonds = diamonds + 2, the computer takes the old number (3), adds 2 to it, and puts the brand new number (5) back in the box.
Chapter 2 Activity: Make Your Own Stats
Section titled “Chapter 2 Activity: Make Your Own Stats”Let’s create a “Character Stat Sheet” for a new game.
- Create variables for your name, health, and money.
- Print the starting stats.
- Change the variables (imagine you found a potion or a treasure chest!).
- Print the new stats.
Example Code:
# Starting the gamehero = "Alex"health = 10robux = 0
print("Player:", hero)print("Health:", health)
# Ouch! You stepped on a cactus!health = health - 1
# Yay! You finished an Obby!robux = robux + 100
print("--- UPDATE ---")print("Health is now:", health)print("Robux is now:", robux)