Tag Archives: variables

Variables in Python and Teaching Coding to Kids

Variables in Python are used by programmers to store values. These values complete a task within a program. In game design variables can track scores, count the number of turns, or store player answers. However, they have many other uses. When teaching coding to kids, it is important that the purpose of a variable is understood.

variables in python

Variables in Python store values that a program uses to complete a task.

What Are Variables in Python?

A variable stores a value that can change. It is saved in a special spot that is like a numbered storage bin. When the program needs the value in the variable it takes it from the bin.

A variable can store lots of different types of information. For example, the value could be a number, text, or a list of items. In programming, a number is called an integer or int for short. Text is called a string or str for short.

A variable has two parts – name and value. To create a variable, you write the variable name, then an = symbol, followed by the variable value. For example: player=”Alex”

variable name and value

A variable has two parts – name and value.

Variables in Python Must Have Meaningful Names

Naming a variable is a fundamental skill. A programmer should be able to read the variable name and understand its use within the program. It is very important that it is short but concise. It’s name should describe its purpose.

A Python variable must:

  • be meaningful
  • be one word
  • have no spaces
  • use no symbols
  • not be a reserved Python word

Why Use a Variable?

Variables make a program flexible. Programmers use them for many reasons.

Variables Can Count the Number of Times an Event Occurs

Variables can count. This is useful when making a timer or tracking a player’s score. To count, you can create a variable called count=0. Each time the line of code count=count+1 is run than the value of the variable goes up by one.

Variables Can Store Multiple Values as a List

Variables can store more than one value. The list may look like this: pickcolor=(“red”, “blue”, “green”). By assigning multiple values to a variable it allows a program to make a choice. This use of variables creates games that are fun to play because the selection is unknown.

Variables Allow the User to Input Information

Sometimes the programmer assigns the value of a variable. Other times, the user inputs a value. This is done by prompting the user to enter data by displaying a text box or question on the screen. For example, the following code will store a user’s name:
name=input(“What is your name?”)

Variables Report Information

By using the variable name in a sentence you can report important information to the user. For instance, to display a player’s score the following code will join text with the variable value:
print(“The game is over. Your score is ” +str(score)).

Variables Personalize the User’s Experience

Variables can be used to communicate, making a device seem more human or less machine-like. Suppose that a player types in their name. Lines of code can have the computer give a personal greeting, such as Hello Sara. The sentence and variable are put together using this Python code: print(“Hello ” +str(player)).

Variables Can Trigger an Action

A variable’s value can change. If it is a number, it can get higher or lower. If it is a text, it could have another value assigned to it. Conditional logic can be used to trigger an action when a variable meets a specific condition. The value of the variable might need to be equal to, greater than, less than, or does not equal. When the variable value matches the condition, then an action will occur. The code might start like this if guess==”answer”:

Variables Calculate Amounts

Variables can be used in mathematical formulas. This has many practical applications. For example, a business owner can track employee earnings. The variables wage, hours_worked, and earnings can be placed into the formula earnings=wage*hours_worked.

Create Artwork and Build Games using Variables in Python to Teach Coding

Learning how to use variables in Python can be fun. In TechnoTurtle, the programming activities gradually introduce elementary and middle school students to variables. In this project they use variables to count loops and create spirographs that are random colors. As well, they store player answers to build a Mad Lib word game, a carnival game that awards a prize, and a guessing game. These programming tasks make the purpose of a variable easy to understand. Beginners experience first-hand how variables are used in a program to complete a task.

technoturtle and variables

Learning about variables can be fun.

How to Keep Score in Scratch, Game Design

Do you need to keep score in Scratch? If yes, keep reading! The instructions explain how to track the points earned by a player each time it touches a target.

Keep score in Scratch

Keep score in Scratch

About the Game

The directions listed below explain how to add scoring to an existing Scratch project. The game should have with a player and target sprite. Several scripts must already be built. For example, the player should have a script that controls its movement using the mouse pointer. As well, the target should be programmed to show in random spots. If your game design is different, you may need to adjust the scoring system to suit your Scratch project.

NOTE: These instructions are modified from the technology project TechnoCode, published by TechnoKids Inc. TechnoCode has programming activities for kids. The lessons include a game design coding unit. The seven assignments, guide students step-by-step through how to construct an entire game. To support learning, the unit has a planning sheet, example videos, sample scripts, programming challenges, game consultant checklist, coding journal log, Scratch quiz, Treasure Hunt skill review, and Variable extension activity.

About the Score Variable

The script you will build requires the use of a variable. A variable is a factor that can change. You will create a score variable. It will be used to hold the number of points.

A coding block will be used to increase the score by a specified number of points. Increase the score by 1

Another coding block will reset the score variable to zero when a new game begins.reset the variable

About the Scoring System Script

Before making the variable and building the script it is important to understand how the scoring system script works. Study the script below.

Is the player touching the target? If yes, then increase the score by 2 points.

score script in Scratch

Code used to change the score in Scratch.

How to Keep Score in Scratch

Keep Track of the Score

  1. Open your saved game project in Scratch.
  2. Select the player sprite.
  3. From the Variables palette, click Make a Variable.
  4. Type score as the variable name. Click OK.
  5. new variable

    Create a score variable.

  6. Select the score variable to display it. score variable
  7. Use your skills to build this script.
    How do you want the script to start? Notice that in the sample script below, it begins with when I receive play game. This is a broadcasted message that you may have already created. If the broadcast message was not made, you can use the when Go clicked Hat block.
  8. Scratch scoring script.

    Set the number of points the player earns each time it touches the target.

    Tip to Setting the Sensor Coding Block:

    • View the Sensing palette.
    • Drag touching mouse pointer touching mouse pointer into the if-then block if-then.
    • Click the arrow and select the name of the target.

    Test the Scoring System

  9. Play the game. When done, click Stop.
    • Does the score increase each time the target is touched?
    • Are too many points added when the player touches a target?


  10. Use these tips to improve how the player scores points:
    • If too many points are added when target touched, add wait 1 seconds after change score.
    • Play a sound when the target is touched. You may need to trim the clip.

    Set the Score to Zero When a New Game Begins

  11. Play the game again. Does the score start at zero? NO, it doesn’t.
  12. Select the player sprite.
  13. From the Variables palette, add set my variable to 0 to the start of the player’s script. Click the Variable arrow and select score.
  14. Add a coding block with the score variable to reset the points.

    WHERE SHOULD THE CODING BLOCK GO? Notice where the set score to 0 block is located in the script above. It was placed after the when Go clicked Hat block of an existing script. Look at your scripts. Where should it be placed in your game?

  15. Play the game again. The score should start at O.