Variables

Variables are a key programming feature. Sometimes the programs we write need to have values that change or need to be stored to recall later on.

We see this often in games.

Tetris

Untitled

Pokémon

pictures courtesy of Enter your name! The best name entry screens in gaming | GamesRadar+

pictures courtesy of Enter your name! The best name entry screens in gaming | GamesRadar+

In both examples above, we can see where a user is expected to enter their name. For for the scoreboard and one so that the game mechanics can keep referring to it during the game story.

Variables in Python

variable_diagram.svg

<aside> 💡 Some golden rules when assigning variables:

  1. Variable names cannot take the name of a python reserved keyword such as print
  2. The name goes on the left ⬅ & the value goes on the right ➡
  3. Just use 1 = . remember that == is a comparative operator.
  4. variable names cannot have spaces. ❎ high score = 145high_score = 145

</aside>

So, if wanted to create a variable called score and assign the value 0, I would write:

score = 0

If I wanted to output the value, I could write:

print(score)

Take a look at how I can assign and re-assign the variable with a new value.

https://codapi.org/embed/?sandbox=python&code=data%3A%3Bbase64%2CK07OL0pVsFUw4Cooyswr0SgG8TW5uIqh4oYo4gA%3D

https://youtube.com/shorts/epkv6Lg5maM?si=wkl-JkD9Lt-nu9Aq

Tasks

Write a program with all of the following variables and then print them to the screen.