SuperRobots

Back to Tutorials

Understanding Variables: Your Robot's Memory

Think about your own memory. You remember your name, your age, and your favorite color. Your brain stores this information so you can use it whenever you need it. Variables work the exact same way for a computer program — they are labeled containers that store pieces of information.

What Is a Variable?

A variable is simply a name attached to a value. In Python, creating one is as easy as writing a name, an equals sign, and the value you want to store.

robot_name = "SuperBot"
health = 100
speed = 3.5
is_alive = True

Here we created four variables: robot_name stores text, health stores a whole number, speed stores a decimal, and is_alive stores a True/False value.

Why Are They Called "Variables"?

The word "variable" means something that can change. The values stored inside can be updated at any time during the program.

health = 100
print(health)  # Output: 100

health = health - 25
print(health)  # Output: 75

The variable health started at 100 but was updated to 75 after the robot took damage. The old value is replaced by the new one.

Naming Your Variables

Good variable names make code easier to read and debug. Follow these rules:

  • Be descriptive: Use enemy_count instead of x
  • Use snake_case: Separate words with underscores like robot_speed
  • No spaces: my variable won't work, but my_variable will
  • Don't start with a number: 1st_robot is invalid, but robot_1 is fine

Using Variables in the Cyber Dungeon

You can create your own variables to write smarter code in the Cyber Dungeon.

steps = 4
for i in range(steps):
    super_robot.moveForward()
super_robot.turnRight()

By storing the number of steps in a variable, you can easily adjust how far the robot walks without rewriting your loop logic. This is the power of combining variables with loops!

Try It Yourself

Open the Cyber Dungeon and experiment with creating your own variables. Try storing the number of enemies to defeat, or create a variable to track how many turns your robot has made. The more you practice, the more natural it becomes.