C. Declaring Variables
Estimated time: 7 minutes | Previous: B. Making a Game Window | Next: D. Making a 3D Cube with Entity
๐ฏ Learning Goals
By the end of this lesson, you'll be able to:
- โ Understand what a variable is
- โ
=can store a value using the symbol - โ Can create variables of various types
Core Concepts
What is a variable?
Variableis a box that stores a value.
๐ Analogy: a box with a name tag
A variable is A box with a name tag on itis the same as!
cube_sizeinto a box with the name tag, put the number2in it- Later,
cube_sizewhen you open the box,2comes out - You can change the contents of the box anytime
Use them to store things like score, health, and color in a game!
Variable usage flowchart
flowchart LR
A["Variable name"] --> B["="]
B --> C["Value"]
C --> D["Stored!"]
E["cube_size"] --> F["="]
F --> G["2"]
G --> H["Store 2 in cube_size"]
style A fill:#e3f2fd,stroke:#1976d2
style E fill:#e3f2fd,stroke:#1976d2
style C fill:#e8f5e9,stroke:#388e3c
style G fill:#e8f5e9,stroke:#388e3c
Creating a variable
my_name = "Student" # String variable
cube_size = 2 # Number variable
cube_color = color.red # Color variable
Description:
=stores a value using the symbol- A variable name is like the label on a box
- Later you can retrieve the value by its variable name
Types of variables
| Types | Example | Description | Use |
|---|---|---|---|
| String | "Student" |
Text wrapped in quotation marks | Names, messages |
| Integer | 2 |
A number without a decimal point | Counts, sizes |
| Mistake | 3.14 |
A number with a decimal point | Positions, speeds |
| Color | color.red |
Ursina colors | Object colors |
โ ๏ธ Variable naming rules
Be careful when naming variables!
- โ
Letters, numbers, and underscores (
_) are allowed - โ
cube_size,myName,player1 - โ Cannot start with a number:
1cube - โ Spaces are not allowed:
cube size - โ Special characters are not allowed:
cube-size,cube@size
Example Code
from ursina import *
app = Ursina()
# Declare variables
my_name = "Student"
cube_size = 2
cube_color = color.red
# Use the variable
print(f"Name: {my_name}")
print(f"Size: {cube_size}")
print(f"Color: {cube_color}")
app.run()
Result
The variable values are printed to the console!
๐ฏ Quiz
cube_size = 2what is the role of the variable cube_sizein?
Show Answer
Answer: B) Stores the number 2
cube_size = 2is cube_sizeinto a variable with this name, the number 2is stored.
Later, cube_sizeIf you use it 2you can retrieve this value!
๐ง Debug Clinic
Problem: NameError: name 'cube_colour' is not defined
Cause: Typo in the variable name (used British spelling)
Fix: Use the exact variable name
Problem: SyntaxError: invalid syntax
Cause: Used a space or special character in the variable name
Fix: Connect words with an underscore (_)
Try Coding It Yourself
Try writing code that declares and uses variables!
Declaring variables
Store the player's name and score in variables.
Example Answer
from ursina import *
app = Ursina()
player_name = "Hero"
player_score = 100
print(f"Player: {player_name}")
print(f"Score: {player_score}")
app.run()
Result: "Player: Hero" and "Score: 100" are printed to the console!
๐งฉ Key Takeaways
What you learned in this lesson:
- Variable: A labeled box that stores a value
=Symbol: Stores the value on the right into the variable on the left- String: Text wrapped in quotation marks (
"Hello") - Number: An integer (
2) or a float (3.14) - Naming rules: Use only letters, numbers, and underscores (cannot start with a number)
Review Checklist
- [ ] Can you explain what a variable is?
- [ ]
=Do you know the role of the symbol? - [ ] Can you tell the difference between a valid and an invalid variable name?
Next step: D. Making a 3D Cube with Entity โ