Learn

Now that we’ve learned what variables are, let’s learn to create and use them.

When creating variables, some computer languages require us to declare them, others create them automatically. For example:

• Java requires variables to be declared.
• Python doesn’t require variables to be declared.

When we declare a variable, we specify a variable name, additional information like the data type, and sometimes the initial value.

In Java a variable declaration looks like this:

``int points;``

In Python, we can create a ‘points’ variable and assign it a value of twenty like this:

``points = 20``

As the game is played and the user wins points, we can update the points, reassigning the variable’s value:

``points = points + 100``

And if we want to print out the points at the end of the game, we can retrieve the variable’s value using its name:

``````print(points)
# Outputs 120``````

This is perfect for counting and printing the points. However, variables are often used to store the values of complex calculations and in the next exercise we will learn about using expressions for calculations!

### Instructions

For this exercise, we don’t have enough blocks to move Codey to the goal with only the `move` commands! Assign variables such as `goalPositionX` or `moveDistance` values by combining the teal number block with the `set` command. Select different variables using the dropdown menu on the set command. Play with the following variables:

• `goalPositionX` to move the horizontal position of the goal (See the starting value on the grid)
• `goalPositionY` to move the vertical position of the goal
• `moveDistance` to change how far Codey goes in the next `move` command (Starts at 1)

There are many ways to solve this puzzle; try exploring!

Hint
• Try moving the goal position closer to Codey
• Try increasing the `moveDistance` to make Codey go farther with each move command
Solution

One possible solution

``````Set moveDistance to 5
Move Right
Move Down``````