Exercise Solutions: Data and Variables¶
Declare and assign variables
Declare and assign a variable for each item in the list.
1 2 3 4 5
shuttle_name = 'Determination' shuttle_speed_mph = 17500 distance_to_mars_km = 225000000 distance_to_moon_km = 38400 MILES_PER_KM = 0.621
Calculate a space mission!
We need to determine how many days it will take to reach Mars.
Create and assign a miles to Mars variable. You can get the miles to Mars by multiplying the distance to Mars in kilometers by the miles per kilometer.
miles_to_mars = kilometers_to_mars * MILES_PER_KM
Next, we need a variable to hold the hours it would take to get to Mars. To get the hours, you need to divide the miles to Mars by the shuttle’s speed.
hours_to_mars = miles_to_mars / shuttle_speed_mph
Finally, declare a variable and assign it the value of days to Mars. In order to get the days it will take to reach Mars, you need to divide the hours it will take to reach Mars by 24.
days_to_mars = hours_to_mars / 24
Now calculate a trip to the Moon
Repeat the calculations, but this time determine the number of days it would take to travel to the Moon and print to the screen a sentence that says
"_____ will take ___ days to reach the Moon.".
1 2 3 4
miles_to_moon = kilometers_to_moon * MILES_PER_KM hours_to_moon = miles_to_moon / shuttle_speed_mph days_to_moon = hours_to_moon / 24 print(shuttle_name + " will take " + days_to_moon + " days to reach the Moon.")