Variables and Text


Collapse Content

A variable in programming is like a container which holds a value:

You can store a value in a variable for later retrieval:

variable with value

You've already seen the color variable, which sets the color of a pen. You can create your own variables for storing numbers, text or anything else. These values can be retrieved later in your program for any purpose. Try these small tasks in the blockly editor to get practice using variables.

Text Variables

Assign and display a variable

  1. Create a variable and change its name to name.
  2. Use a Text block to assign its value to the name that you want.
  3. Use the print block to display the value of name in a pop-up.
Video Walk-through

User input

Instead of setting the value of name in your code, let's ask the user for their name. Replace the 'hard-coded' text block with a prompt block that gets input from the user. You can optionally add a message to display in the prompt, such as "Your name please?".

Your print block will now display the user's name in the pop-up.

Blockly Text Blocks

Creating new variables

Instead of just showing the user their name, can you greet them with a custom message?

  1. Create a new variable called message and attach it below the name variable block.
  2. Attach the "create text with" block to your message variable to assign it a value.
  3. Use this block to combine the text "Hello " with name.
  4. Change your print block so it prints message instead of name.
Video Walk-through

Comments

  • None of the Blockly levels seem to count towards or save progress, and on this node there's no way to run the code you create!

  • Thanks for reporting that issue, the run button had been covered up, but it's now fixed. Sorry that no check mark appears on the side, this is just meant as a short stand-alone intro to programming with blockly.

  • Thank you for the quick fix... the next node has the same issue btw. I've used Blockly with students in the past and love the way you present these tutorials, I'm just looking forward to the check marks appearing so that I can track my students progress through the levels.

  • Thanks for the feedback, fixed. I'll look into some way students can share the progress.

Contact Us
Sign in or email us at [email protected]