Answer #2 | Python Cram Kit

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

Answer G: float

Answer H: i, dest in enumerate(destinations)

Answer I: total_trip_cost = price_of_gas * miles_to_destination[i] / mpg

Answer J: total_trip_cost == None

Let's break down why each answer is what it is.

# 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? "))

# Initialize variable to store the cost of the trip
total_trip_cost = None

# Match the user_destination to destinations. Then, find the total trip cost using the miles
for ? - Answer H - ?:
    if user_destination == dest:
        ? - Answer I - ?

# If the total trip cost is still null, the user didn't enter a valid destination
if ? - Answer J - ?:
    print("You didn't enter a valid destination. Please try again.")

# If the total trip cost is present, tell the user how much it'll cost
else:
    print("With the price of gas at $" + str(price_of_gas) + ", the trip will cost a total of $" + format(total_trip_cost, ",.2f") + ".")

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))

Answer H Explanation

Let's first start out by determining what we're going to be for-looping through in Answer H.

By looking at the comment above "? - Answer H - ?", we can determine that within this for loop, we're going to be using both the destination and the miles to that destionation to determine the total cost of the trip.

...
# Match the user_destination to destinations. Then, find the total trip cost using the miles
for ? - Answer H - ?:
...

This means that while we're looping through the destinations themselves, we're also going to need to reference the "Miles to destination" values for each destination. These values are stored in a different array than destinations, which is why we actually need to utilize the "For loop variations ...with multiple arrays" Code Template.

Answer H: i, variable in enumerate(array)

This way, we can utilize the counter variable of i to reference values in the miles_to_destination array while we go through each destination!

So, what should our variable be? In other words... what should we call each destination?

To solve this, we must look one line of code below.

...
for ? - Answer H - ?:
    if user_destination == dest:
...

user_destination is already defined earlier when we ask the user for their destination of choice, but dest has not been defined yet. That's how we know we need to place it as our variable!

Answer H: i, dest in enumerate(array)

Now, what's our array going to be? This one is easy... it's destinations array, since those are what we're looping through!

Answer H: i, dest in enumerate(destinations)

Answer I Explanation

If the explanation for Answer H didn't make sense to you, this'll clear it up.

For Answer I, we're going to primarily utilize the formula given in the question to calculate the total cost of the trip.

First, we must decide what name to give the variable that'll hold (total trip cost).

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

To do this, we must first look ahead in the code and see if it's already defined!

After scanning through the code, we can see it defined as total_trip_cost at the end of the program.

...
# If the total trip cost is present, tell the user how much it'll cost
else:
    print("With the price of gas at $" + str(price_of_gas) + ", the trip will cost a total of $" + format(total_trip_cost, ",.2f") + ".")

Therefore, we'll define the total cost of the trip as total_trip_cost.

Answer I: total_trip_cost =

Perfect, let's move onto the (current price of gas).

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

If you read through the question again, you'll notice that the user actually inputs this value. Where in the code can we find this?

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

Using this, we can know that we must place price_of_gas as the (current price of gas) in our answer!

Answer I: total_trip_cost = price_of_gas *

Now for the (miles to destination)...

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

...this one is a little tough. Remember what we did before in Answer H? We used the enumerate() function to enable us to reference the variable i within our for loop.

Answer H: i, dest in enumerate(destinations)

What does this variable i really do though? It keeps track of the current index that we're on within our loop.

How's this useful? For each dest, it will enable us to reference the miles_to_destination value corresponding to it!

We can do this by using the following format: array[index]

Answer I: total_trip_cost = price_of_gas * miles_to_destination[i] /

Now for the last part...

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

Where do we reference the mpg that our car achieves? At the top of the program!

...
# Define the car's mpg
mpg = 25
...

Therefore, we'll just place mpg as our (25mpg) value.

Answer I: total_trip_cost = price_of_gas * miles_to_destination[i] / mpg

Answer J Explanation

For Answer J, it is crucial to read the comment above it to understand the context.

...
# If the total trip cost is still null, the user didn't enter a valid destination
if ? - Answer J - ?:
...

Where would total_trip_cost be defined as null (a.k.a. "None" in Python)? Scan through the code and try to find it.

...
# Initialize variable to store the cost of the trip
total_trip_cost = None
...

It's located a little above "? - Answer J - ?". So, how can we use it to figure out what to put for Answer J?

By reading the comment, stating that it's "Initializ[ing] variable to store the cost of the trip", we can comprehend that this code is setting the starting value of total_trip_cost to "None", or null.

So, why would total_trip_cost stay null? I thought we eventually define it with our formula for the total trip cost??

Well, we do... if the user enters a valid destination. Notice how if the user's destination...

for i, dest in enumerate(destinations):
    if user_destination == dest:
        total_trip_cost = price_of_gas * miles_to_destination[i] / mpg

...doesn't exist within as one of the destinations, resembled as dest each loop...

for i, dest in enumerate(destinations):
    if user_destination == dest:
        total_trip_cost = price_of_gas * miles_to_destination[i] / mpg

...the if statement never executes the code within it?

for i, dest in enumerate(destinations):
    if user_destination == dest:
        total_trip_cost = price_of_gas * miles_to_destination[i] / mpg

And if the code in the if statement never executes... then the value of total_trip_cost stays "None"!

Therefore, within Answer J we must assess if total_trip_cost still equals "None".

Answer J: total_trip_cost == None

If this still doesn't make sense, reference the comment above "? - Answer J - ?" again. Notice how it legit says to assess "If the total trip cost is still null"?

...
# If the total trip cost is still null, the user didn't enter a valid destination
if ? - Answer J - ?:
...

Leave a Comment