Login

Please fill in your details to login.





lesson 3.1.2 variables and user input

You remember my name?!



image

Have you ever played a game that remembers your high score or greets you by name? That's thanks to variables! Today, we're swapping code blocks for Python text and learning how to store information using variables. We'll also find out how to make our programs interactive by asking the user for input. These are core skills every Software Developer uses daily!

Learning Outcomes
The Building Blocks (Factual Knowledge)
Recall that a variable is a named location for storing data.
Describe the purpose of the input() function for getting user data.
Recall that a string is a sequence of characters (text).

The Connections and Theories (Conceptual Knowledge)
Describe how programs use variables to store information that might change.
Analyse the need for meaningful variable names to make code understandable.
Apply the concept of sequence when writing simple Python programs.

The Skills and Methods (Procedural Outcomes)
Create simple Python programs using print() for output.
Apply variable assignment (=) to store different values, including user input from the input() function.
Apply string concatenation (+) to join strings and variables together for output.

Digital Skill Focus: Use a keyboard effectively to type Python code accurately [C.1.1].

What is a Variable?


In programming, we need a way to store information that our program can use later. We might need to remember a player's score, a user's name, or the cost of an item. For this, we use a variableA variable is a temporary value held in RAM and assigned a name / identifier. The value of the variable can change during program execution..

You can think of a variable as a labelled box in the computer's memory.

The label on the box is the variable's IDENTIFIER or NAME (e.g.,
score
).
The thing you put inside the box is the variable's VALUE (e.g.,
2500
).

The great thing about variables is that their values can vary (change!) while the program is running. We can look inside the box, change what's inside, or add to it. In Python, we create a variable and give it a value using the equals sign (
=
), which is called the assignment operatorA mathematical operator used to assign a value to a variable or a constant. Usually an equals (=) sign or a leftward facing arrow (←) in pseudocode..

Try running the following Python code by clicking the ▶️ button...

🤔 Code Explainer
Line 1: This line creates a variable called
player
and stores the text
Alex
in it.
Line 2: This line creates a variable called
score
and stores the integer
2500
in it.
Line 3: Print the value of variable
player
to see what's inside.
Line 4: Print the value of variable
score
to see what's inside.

Notice that text values are surrounded by quote marks ("). This tells Python that it is a piece of text, which we call a stringA sequence of characters delimited with quotation marks.. Numbers don't need quotes. A whole number is called an integerA whole, positive or negative number..


time limit
Task 1 Code Detectives

Your teacher has shown you how variables work. Let's look at how this works in Python. Remember that each Python widget has a list of PRIMM tasks underneath it.

1
Try it out!

Now, look at the Python code snippet below. It does the same thing as the activity you just did. Follow the guidance under the IDEI have no idea what this means (do you remember what that means?)

🤔 Code Explainer
Lines 1-3: Store some values in variables.
Line 4: Create a sentence using the variables.
Line 5: Print the final sentence

1
USE
What will the code do when you click ▶️?
Run the script by clicking the ▶️ button.
Were your predictions right?
2
MODIFY
Remove the
+
signs from the expression on line 4.
What happens when you run the script now?
3
CREATE
Create a new variable called
dinner
Give it a value and concatenateI have no idea what this means it to the end of the
sentence
variable.

2
Answer the questions in your head...

1
How many variables are created in this program?
2
What is the name of the variable that stores the value "Tiger"?
3
What is the value stored in the variable named
city
?
4
What does the
+
symbol do when used with strings like this?
5
Can you see any problems with the code? What would the output be if the animal was "elephant"? Would it be grammatically correct?

Outcome: I can read a simple program and identify the names and values of variables, and I can predict the output of a program that joins strings together.

Checkpoint

Getting User Input


It's not very useful if our programs can only use values we type directly into the code. We need a way to ask the user for information while the program is running. In Python, we use the
input()
function for this.

The
input()
function shows the user a message (called a prompt) and then waits for them to type something and press Enter. Whatever the user types is then returned as a string, which we can store in a variable.

Run the following script using the blue play button ▶️. Type your name and press the ENTER key. You can rerun the script by pressing ▶️ again.

🤔 Code Explainer
Line 1: The message inside the brackets is the prompt shown to the user. When the user types their name and presses enter, Python stores the value in a variable called
name
.
Line 2: We can use it to print a personalised message.

This process of joining strings together is called concatenationTo join together as in string concatenation..


time limit
Task 2 Mad Libs Story Generator
Let's build a program that creates a silly story using words from the user. We will use the UMC model: USE, MODIFY, CREATE. Remember to check out the 'Code Explainer' and follow the UMC instructions carefully, one step at a time!

🤔 Code Explainer
Line 1: Print a lovely title.
Lines 2-4: Ask the user for some words and store them in variables.
Line 5: Create the story by joining the strings together.
Line 6: Print the final story.

1
USE
What do you think the code will do?
What will it ask, and what will the final output look like?
2
MODIFY
Look at the line
name = input("Enter a name: ")
. What does the
input()
function do?
Find the line that creates the
story
variable. What does the
+
symbol do here?
Why do we need "
 
" (a space inside quotes) between the variables in the story? What would happen if we removed them?
Change the story to make it even funnier.
Add one more question at the start (e.g., ask for a type of food) and include the user's answer in your story.
3
CREATE
Create a brand new Mad Libs program in the clean editor below.
It should ask the user for at least five different words (e.g., an adjective, a verb, a place) and then use them to tell a short story.
Use the Print to PDF button to print out a copy for your teacher.


Outcome: I have UMC'd and possibly created a Python program that asks the user for several pieces of text input and uses them to generate a personalised story.

Checkpoint

image
Today, you've learned the basics of storing information in Python using variables and how to make your programs interactive using the
input()
function. You've also practised joining strings together using concatenation.

Out of Lesson Learning

Last modified: September 17th, 2026
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning