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..

🤔 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 integerA whole, positive or negative number.
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. There is also a Thonny based extension activity if you fancy a challenge...

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
Predict
What will the code do when you click Run Script?
2
Run
Run the script by clicking the Run Script button.
Were your predictions right?
3
Investigate
Remove the
+
signs from the expression on line 6.
What happens when you run the script now?
4
Modify
Alter the story by changing the values of the variables.
5
Make
Create a new variable called
dinner
Give it a value and concatenateTo join together (verb). it to the end of the
sentence
variable.

2
Answer the questions

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.

🤔 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 PRIMM model: Predict, Run, Investigate, Modify, Make.

1
Everyone ready?

🤔 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
Predict
What do you think the code will do?
What will it ask, and what will the final output look like?
2
Run
Was your prediction correct?
If not, can you explain what really happened?
3
Investigate
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?
4
Modify
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.
5
Make
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.


2
Not challenging enough?

I've left the Copy button on the first widget in step one. Try copying the code into a new .py file in Thonny and seeing if you can run it there. Noice.

Outcome: I have PRIMM'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: October 22nd, 2025
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning