Chapter 2: The Magic Inventory
[cite_start]Ten-year-old Leo spent three days building the ultimate pizza restaurant in Roblox, but there was one huge problem. [cite: 98] [cite_start]Every time a player bought a slice of pepperoni, the game completely forgot to give them their points! [cite: 99] [cite_start]Leo realized that without a way for the game to remember things, his awesome pizza parlor was totally broken. [cite: 100]
[cite_start]In the last chapter, you learned how to give the computer orders using print. [cite: 101] But right now, your computer has a short memory. [cite_start]It does exactly what you say, then forgets everything immediately! [cite: 102] [cite_start]To make a real game, the computer needs to remember things. [cite: 103]
[cite_start]In Python, we use something called a : . [cite: 106, 172] [cite_start]A variable is like a labeled box where the computer stores information to use later. [cite: 107]
Creating a Variable
Section titled “Creating a Variable”Creating a variable is like labeling a chest and putting something inside it. [cite_start]We need two things: [cite: 108, 109]
- The Name: What is on the label? (e.g.,
score,gold,player_name) [cite_start][cite: 110] - The Value: What is inside the box? (e.g.,
100,"Steve") [cite_start][cite: 111]
[cite_start]In Python, we use the : (the equals sign =) to put things in the box. [cite: 112, 172]
[cite_start]Minecraft Example: [cite: 113]
blocks = 64- [cite_start]The Label:
blocks[cite: 114] - [cite_start]The Value:
64[cite: 115] - [cite_start]The Translation: “Computer, create a box named ‘blocks’ and put the number 64 inside it.” [cite: 116]
[cite_start]Roblox Example: [cite: 117]
run_speed = 16- [cite_start]The Label:
run_speed[cite: 118] - [cite_start]The Value:
16[cite: 119]
[cite_start]Real-World Example: Think about your cubby or a locker at school. [cite: 120] [cite_start]The label on the outside has your name on it, and the “value” inside is your backpack, your jacket, and maybe a squished sandwich. [cite: 121] [cite_start]You always know exactly where to find your stuff because of that label! [cite: 122]
Using Your Variables
Section titled “Using Your Variables”Once you have stored something, you can use it! [cite_start]You don’t need to type the number 64 over and over again. [cite: 124] [cite_start]You just use the variable name. [cite: 125]
Let’s try a script to introduce a player. [cite_start]Type this code: [cite: 126]
player_name = "NoobGamer123"score = 0
print("Welcome,")print(player_name)print("Your current score is:")print(score)[cite_start]What happens: [cite: 127]
[cite_start]The computer looks inside the variable player_name, finds "NoobGamer123", and prints it. [cite: 128] [cite_start]Then it looks inside score, finds 0, and prints that! [cite: 129]
Combining Words and Variables
Section titled “Combining Words and Variables”[cite_start]Printing on separate lines is okay, but it looks a bit robotic. [cite: 131] [cite_start]Let’s make it look like a real game message by putting words and variables on the same line. [cite: 132]
[cite_start]To do this in Python, we use a comma ,. [cite: 133] [cite_start]The comma acts like glue—it sticks your message and your variable together. [cite: 134]
[cite_start]Try this: [cite: 135]
my_pet = "Dragon"coins = 50
print("You equipped your", my_pet)print("You have", coins, "coins")[cite_start]Output: [cite: 136]
You equipped your DragonYou have 50 coinsNotice how Python automatically adds a little space where the comma was? [cite_start]It keeps your sentences neat! [cite: 137]
[cite_start]Real-World Example: Imagine writing a birthday card where you fill in the blanks. [cite: 138] “Happy Birthday, [Name]! You are turning [Age] today!” [cite_start]The words on the card stay exactly the same, but the blanks are like variables. [cite: 139] [cite_start]You can glue in “Sarah” and “10” or “Tommy” and “9”, and the sentence still makes perfect sense. [cite: 140]
Updating Variables (Looting and Leveling Up!)
Section titled “Updating Variables (Looting and Leveling Up!)”Variables are powerful because they can change. [cite_start]In fact, the word “variable” actually means “able to vary” (change). [cite: 142]
[cite_start]We can change a variable by doing a little bit of math. [cite: 145]
[cite_start]The Math of Gaming: [cite: 146] [cite_start]Let’s say you found some diamonds. [cite: 147]
diamonds = 3print("Diamonds found:", diamonds)
# You mined 2 more!diamonds = diamonds + 2print("Total diamonds:", diamonds)[cite_start]Output: [cite: 148]
Diamonds found: 3Total diamonds: 5[cite_start]When 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. [cite: 149]
<CodingActivity title=“Make Your Own Stats”> [cite_start]Let’s create a “Character Stat Sheet” for a new game. [cite: 151]
- [cite_start]Create variables for your name, health, and money. [cite: 152]
- [cite_start]Print the starting stats. [cite: 153]
- [cite_start]Change the variables (imagine you found a potion or a treasure chest!). [cite: 154]
- [cite_start]Print the new stats. [cite: 155]
[cite_start]Example Code: [cite: 156]
# 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)</CodingActivity>