Learn

We’ve learned how to count up the player’s points but what happens if the player wins bonus points at the end of a round. How do we calculate them?

We can use expressions for this.

Expressions

Expressions are programming statements that are evaluated and converted into a value. The expression below returns a value of 300 by using the ‘+’ operator to add numbers together:

100 + 200

Expressions can use variables. The expression below returns the value of the points variable and 100 added together:

points + 100
Assigning Variables Using Expressions

Now we can assign a new ‘bonus points’ variable that rewards the player for completing the round quicker:

bonus_points = 10000 / time_taken_seconds # Set bonus points to 1000 divided by the number of seconds it took to win the level

Keep in mind that some numbers are different from others. For example, some numbers are only whole numbers, others can have decimal points.

Most languages are smart enough to store the values after the decimal point when required. If we were writing a Python program to convert Fahrenheit temperatures to Celsius, we could use two variables:

fahrenheit = 55 celsius = (fahrenheit - 32) * 5 / 9 print(celsius) # prints out 12.777777777777779

So now we know how to calculate the points in the game using numeric variables.

That helps us write our game but we often need other types of variables in our other programs. We will discuss them next.

Instructions

In this exercise, we’ve added operators that can modify our moveDistance and goalPosition variables using mathematical operators such as:

  • Add
  • Subtract
  • Multiply
  • Divide

Remember moveDistance starts at 1, and we can find the goal position by reading the grid.

Use these operators to get Codey to their goal.

Hint

One option is to use multiply or add on moveDistance. Another is to subtract or divide the goal positions.

Solution
Add 5 to moveDistance Move Right Add 1 to moveDistance Move Down

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?