Introduction to Programming

Activity 2: Saving User Inputs in Variables

Lesson 4: Variables

In the previous activity, we used variables to track a sprite's position automatically. In this activity, we will take it a step further — we will ask the user for input and save their response in a variable. This makes our programs interactive and personalized!

PictoBlox can ask the user a question and wait for them to type an answer. The answer is temporarily stored in a special block called 'answer', which we can then save into a variable.

Setting Up

  • Open PictoBlox and create a New File.
  • Select Block Coding as the coding environment.
  • Choose any sprite you like — we will use Tobi for this activity.

Creating the Variables

  • Go to the Variables palette and click Make a Variable.
  • Create a variable called Name.
  • Create another variable called Age.
  • Make sure both variables are set to For all sprites.

The Ask Block

The 'Ask () and wait' block is found in the Sensing palette. When this block runs, a text input box appears at the bottom of the Stage, and the program pauses until the user types an answer and presses Enter.

Key Blocks for This Activity

BlockPaletteWhat it Does
Ask () and waitSensingDisplays a question and waits for the user to type an answer
AnswerSensingStores the most recent answer typed by the user
Set (variable) to ()VariablesSaves the answer into a variable
Say () for () secondsLooksMakes the sprite display a message in a speech bubble

Building the Script

Now let's build the script that asks the user for their name and age, saves both into variables, and has Tobi greet them. You will find the blocks in the mentioned palettes:

  • When flag clicked — Events
  • Ask (What is your name?) and wait — Sensing
  • Set (Name) to (answer) — Variables
  • Ask (How old are you?) and wait — Sensing
  • Set (Age) to (answer) — Variables
  • Say (join (Hello ) (join (Name) (! Great to meet you!))) for (3) seconds — Looks & Operators
Block coding script showing when flag clicked, ask What is your name and wait, set Name to answer, ask How old are you and wait, set Age to answer, then say a joined greeting message for 3 seconds
Script to ask the user for their name and age and save the responses in variables

Running the Script

  • Click the green flag to start the program.
  • A question box will appear at the bottom of the Stage — type your name and press Enter.
  • The next question will appear — type your age and press Enter.
  • Watch Tobi greet you by name using the values stored in the variables!
PictoBlox stage showing Tobi the bear with a speech bubble greeting the user by name, with Name and Age variable values displayed on the Stage
Tobi greeting the user using the values saved in the Name and Age variables
The 'answer' block only holds the most recent response. That's why we save it into a variable immediately after each 'Ask and wait' block — so we don't lose it!

Challenge

Try extending the script to ask the user one more question — such as their favorite color or their favorite animal — and have Tobi respond with a fun message using their answer!

Save your file with the name Saving User Inputs before moving on to the next activity!

Subscribe to our newsletter.

Get updates to news and events.