πŸ“‰ Updating Variables

Why Variables Change

A variable is not locked to one value forever. When you assign a new value to a variable, it replaces the old one. For example:
speed = 10
speed = 25
print(speed)
The program prints 25 because the second assignment overwrote the first. This process is called reassignment.
In World of Software Engineering, our hero takes damage during battle. Instead of creating a new variable each time, we update the same one. That way, the variable always stores the hero’s current health.
Assignment:

Our hero starts with 1000 health. Before each print() statement: Reduce player_health by 100. The health should decrease step by step each time it is printed.
Update the health value
player_health = 1000

# reduce by 100 here

print(player_health)

# and here

print(player_health)

# and here

print(player_health)

# and here

print(player_health)

← Previous Home Next β†’