Variables are named symbols that can store data. In some languages you are required to specify a type for a variable before you store data in it. A type determines whether the variable can hold a number, string, boolean value, etc. Scratch, however, is a weakly typed language. This means that the variable type does not have to be specified. To create a new variable, click on the Variables tab and then on the "Make a variable" button.
You can create a variable for all sprites or for only the currently selected sprite. A variable for all sprites is known as a global variable in most programming languages. A variable for only one sprite is a local variable. For now, let's create a global variable called X. Once you have created a variable you will notice that it appears in the Variables panel and on the Stage (it can be taken off the stage by clicking the check box next to it or by using the show/hide statements but let's leave it alone for now). You can set a variable to a certain value by using "set" and you can increment or decrement it by a value by using "change."
Let's load a value from a user and then echo it to the screen. Start with the Green Flag event. Now select the "Sensing" tab and attach the "ask" block to the Green Flag event:
Use this tab to ask "Enter a number:".
The "answer" block will contain the result of this query when the program is run. Let's store answer in the variable X. To do this, go to the variables tab and attach the "set X" block to the last command in the Script. Now go back to "Sensing" and drop the "answer" block into the operand box of "set X":
Take the square root of the answer and return it on the screen with the text "The square root of " write the number here using join " is " write the square root here using join. Note that you need three nested joins for this and the "say" block in Looks:
Hit the Green Flag button to test the script: