Astronomy Hour of Code
Activity 10: Turning a text value to an integerInstructions:
The type of a variable can sometimes be changed to another type. For example, we might want to turn a text value, like "5" to an integer value, like 5. In order to do this, we have to use the cast command and then tell the computer a) what type to convert the variable to and b) which variable to convert.
The cast command looks like this:
integer value = cast(integer, "5")
When we ask the user a question using the input command, the type of the user's response is always text, so if we want to use input from a user in a numeric formula, we must convert it to a number first. To complete this activity, ask the user to enter an integer and store the value in an integer variable called value and then output the variable value. The first line will look like this:
integer minutes = cast(integer, input("How many minutes: "))
Next, turn the minutes into seconds by multiplying minutes by seconds like this:
integer seconds = minutes * 60
On the final line, use the output command to display the user's answer:
output seconds
When you are ready, select the green Run Program button below the editor on the right (second in the tab order).
Exploration Challenge (Optional):
Add code to get values for hours and seconds from the user like you did for minutes. Then output the total in a user friendly format like: There are 6503 seconds in 1 hours and 48 minutes and 23 seconds. (Hint: you will have to be careful to make sure you do not write over the user's response for seconds when you calculate the total seconds.)
Next Tutorial
In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.