Practice Problems (Sneak Peek) | Python Cram Kit

Question #2 (Sneak Peak)

You want to create a program that will help you determine where you and your friends should go on your trip this summer. You've created a list of destinations, each including the number of miles away from your current location. Here is the resulting list:

DestinationMiles to destination
Zion1,774.3
Grand Teton1,569.8
Yosemite2,213.6
Banff1,954.8
Acadia1,148.9

The program will prompt the user to select a destination from the list above. Then, the program will ask the user what the price of gas is currently. If they enter an invalid destination, it will alert them.

The car that you're driving achieves 25 mpg. After collecting the user's inputs, the program will calculate (using the formula below) and display the approximate cost that the trip will cost in gas.

(total trip cost) = (current price of gas) * (miles to the destination) / (25mpg)

# Define the destinations
destinations = ["Zion", "Grand Teton", "Yosemite", "Banff", "Acadia"]
# Define the miles to each destination
? - Answer F - ?
# Define the car's mpg
mpg = 25

# Ask the user for their destination
user_destination = input("What is your destination? Please enter 'Zion', 'Grand Teton', 'Yosemite', 'Banff', or 'Acadia'. ")
# Ask the user for the price of gas
price_of_gas = ? - Answer G - ?(input("What's the price of gas? "))
...

Answer #2 (Sneak Peak)

Answer F: miles_to_destination = [1774.3, 1569.8, 2213.6, 1954.8, 1148.9]

Answer G: float

...

Answer F Explanation

Answer F is a little tough to figure out on its own... we must use the destinations above it to help us figure out how to write it.

# Define the destinations
destinations = ["Zion", "Grand Teton", "Yosemite", "Banff", "Acadia"]
...

Notice how all the program does here is utilize an array to list out each location within the table?

DestinationMiles to destination
Zion1,774.3
Grand Teton1,569.8
Yosemite2,213.6
Banff1,954.8
Acadia1,148.9

We're just going to do the same exact thing here, but with the "Miles to destination"!

DestinationMiles to destination
Zion1,774.3
Grand Teton1,569.8
Yosemite2,213.6
Banff1,954.8
Acadia1,148.9

But wait... before we just jump right in and start writing our answer, we must first decide what to name the variable that will hold the "Miles to destination" values. Why?

Because if we name it, but later in the program it's already named something else, we won't get a point for this answer!

Therefore, we must scan through the rest of this program and see if there's a variable defined that represents "Miles to destination".

After scanning through the code, we can confirm that there's not a variable that holds these values already references. Therefore, we can name this variable whatever we want. So, we'll name it miles_to_destination and write the values of "Miles to destination" in it within an array.

Answer F: miles_to_destination = [1774.3, 1569.8, 2213.6, 1954.8, 1148.9]

Answer G Explanation

For Answer G, the big thing to notice is the contents of our question within the input statement.

...
# Ask the user for the price of gas
price_of_gas = ? - Answer G - ?(input("What's the price of gas? "))
...

It's focusing on the "price of gas"... which is typically written as 2.35, 3.41, 1.89, etc.

While these values of the "price of gas" are different, they have one main thing in common...

They're all floats!

Therefore, using the "Input statement variations ...with floats" Code Template, we know to place "float" as our answer!

Answer G: float

That way, the program's code matches the "Input statement variations ...with floats" Code Template!

float(input(question))