Astronomy Hour of Code

Activity 9: Using more than one variable in a formula

Instructions:

We can also use more than one variable in a formula or mix variables and numbers. In this activity, we are going to set up a formula to calculate the total number of seconds when we are given a certain number of minutes and seconds. To do this, start by creating two integer variables: minutes and seconds and assigning values to them. Let's assign a value of 3 to minutes and 23 to seconds like this:

integer minutes = 3
integer seconds = 23

Now we can output the total number of seconds by using a formula to compute the answer by multiplying minutes by 60 (since there are 60 seconds in a minute) and then adding seconds. Our formula will look like this:

output minutes * 60 + seconds

When you click the green Run Program button, the number 203 should display in the output console.

Exploration Challenge (Optional):

Modify the output statement so that the computer gives you a user-friendly message like: There are 203 seconds in 3 minutes and 23 seconds. You will have to put your formula in parenthesis in your output statement to get the correct answer...can you guess why this is necessary? After you have the friendly message working, change the variable values to find out how many seconds are in 5 minutes and 10 seconds without changing your output statement. (Hint: you must use the integer variables in the output statement.)

Next Tutorial

In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.