Question

You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...

You previously wrote a program that calculates and displays:

Miles driven

Miles per gallon

Kilometers driven

Liters of fuel consumed

Kilometers per liter

Messages commenting on a vehicle's fuel efficiency

And also validates input

Now we'll make a somewhat major change. We'll remove the code for obtaining input from a user and replace it with hardcoded data stored in lists. Because the data is hardcoded, the program will no longer need the code for validating input. So, you may remove that, as well.

Since the program needs three lists representing the items of input – mileage at beginning of the measurement period, mileage at end of the period and gallons of fuel consumed – we'll create three lists – one for beginning mileage, a second for ending mileage and a third for gallons consumed – and hardcode the data in them. By iterating through a loop which has statements for retrieving elements from these lists, the program will obtain the input it needs for performing calculations and producing output. To make this easy, let's use these lists:

beginning_miles_list = [ 100, 200, 300, 400, 500, 600 ]

ending_miles_list = [ 400, 500, 600, 700, 800, 900 ]

gallons_used_list = [ 25.0, 18.75, 13.64, 10.71, 9.38, 8.33 ]

If you feel daring and innovative, use a two-dimensional list:

fuel_usage_list = [] # just a plain empty list

fuel_usage_list.append( beginning_miles_list )

fuel_usage_list.append( ending_miles_list )

fuel_usage_list.append( gallons_used_list )

See the attached program Fuel_Efficiency_2D.py for more on this.

Create the lists and load data into them before the loop.

Except for simplified academic situations, like this one, we almost never Hardcode data in programs. Fear not, later we'll obtain data more realistically by reading it from a file.

Language: PYTHON

Attached file with the question:


# create a list for each set of data values

beginning_miles_list = [ 100, 200, 300, 400, 500, 600 ]

ending_miles_list = [ 400, 500, 600, 700, 800, 900 ]

gallons_used_list = [ 25.0, 18.75, 13.64, 10.71, 9.38, 8.33 ]

# create an empty list to hold the 3 previous lists

fuel_usage_list = []

fuel_usage_list.append( beginning_miles_list ) # append the beginning miles list to fuel_usage_list.

fuel_usage_list.append( ending_miles_list ) # append the ending miles list to fuel_usage_list.

fuel_usage_list.append( gallons_used_list ) # append the gallons used list to fuel_usage_list.

# now fuel_usage_list is a list of 3 elements, or rows, each containing a list of 6 elements, which are the columns

print("\n\n")

for row_index in range( len( fuel_usage_list ) ):

for column_index in range( len( fuel_usage_list[ row_index ] ) ):

print( fuel_usage_list[ row_index ][ column_index ], "\t", end="") # end="" prevents newline from being wriiten

print("\n") # print a new line here to delineate rows

My actual code:

name = input("\nHello Dear customer, please enter your username : ")

def take_input(s):

value = 0

while True:

try:

value = float(input(s))

if value <= 0:

print("Enter a valid positive numeric value")

value = take_input(s)

break

except ValueError:

print("Enter a valid positive numeric value")

return value   

MB = take_input("\nPlease enter the mileage at Mileage at beginning of measurement period: ")
  
MEP = take_input("\nPlease enter the Mileage at end of measurement period: ")

GFC = take_input("\nPlease enter the Gallons of fuel consumed: ")

print("\nThank you! please wait...")

print("\nHere are your results:")

MilesDrive = (MEP - MB)

MPG = (MilesDrive / GFC)

KM = (1.60934 * MilesDrive)

FC = (GFC * 3.785412)

KML = (KM / FC)

print("\nYou drived: ", + MilesDrive,)

print("\nYou consumed: ", + MPG, "MPG")

print ("\nYou drived: ", + KM, "kilometrs",)

print("\nDuring your trip you consumed: ", + FC, "liters")

print("\nDuring your trip you consumed: ", + KML, "Liters per Kilometer")


if MPG < 15:
print("\nYour vehicle has criminally low fuel efficiency.")

elif MPG >= 15 and MPG < 20 :
print( "\nYour vehicle has undesirably low fuel efficiency.")

elif MPG >= 20 and MPG < 25 :
print("\nYour vehicle gets barely acceptable efficiency.")

elif MPG >= 25 and MPG < 35 :
print( "\nYour vehicle gets high fuel efficiency.")

else:
print("\nYour vehicle gets outstanding fuel efficiency.")
  
print("\nThank you for visit our website, have a great date, " + name, "!")

0 0
Add a comment Improve this question Transcribed image text
Answer #1
beginning_miles_list = [100, 200, 300, 400, 500, 600]
ending_miles_list = [400, 500, 600, 700, 800, 900]
gallons_used_list = [25.0, 18.75, 13.64, 10.71, 9.38, 8.33]

fuel_usage_list = []
fuel_usage_list.append(beginning_miles_list)  # append the beginning miles list to fuel_usage_list.
fuel_usage_list.append(ending_miles_list)  # append the ending miles list to fuel_usage_list.
fuel_usage_list.append(gallons_used_list)  # append the gallons used list to fuel_usage_list.
# now fuel_usage_list is a list of 3 elements, or rows, each containing a list of 6 elements, which are the columns

for row_index in range(len(fuel_usage_list)):
    for column_index in range(len(fuel_usage_list[row_index])):
        print(fuel_usage_list[row_index][column_index], "\t", end="")  # end="" prevents newline from being written
    print()  # print a new line here to delineate rows

row_index = 0
for column_index in range(len(fuel_usage_list[row_index])):

    # Calculate using the formulas you used before
    # Extract mileage in beginning, ending and gallons from fuel_usage_list
    MilesDrive = (fuel_usage_list[row_index + 1][column_index] - fuel_usage_list[row_index][column_index])
    MPG = (MilesDrive / fuel_usage_list[row_index + 2][column_index])
    KM = (1.60934 * MilesDrive)
    FC = (fuel_usage_list[row_index + 2][column_index] * 3.785412)
    KML = (KM / FC)

    print("\nHere are your results:")
    print("You drived: ", + MilesDrive, )
    print("You consumed: ", + MPG, "MPG")
    print("You drived: ", + KM, "kilometers", )
    print("During your trip you consumed: ", + FC, "liters")
    print("During your trip you consumed: ", + KML, "Liters per Kilometer")

    if MPG < 15:
        print("\nYour vehicle has criminally low fuel efficiency.")
    elif MPG >= 15 and MPG < 20:
        print("\nYour vehicle has undesirably low fuel efficiency.")
    elif MPG >= 20 and MPG < 25:
        print("\nYour vehicle gets barely acceptable efficiency.")
    elif MPG >= 25 and MPG < 35:
        print("\nYour vehicle gets high fuel efficiency.")
    else:
        print("\nYour vehicle gets outstanding fuel efficiency.")
    print()

SCREENSHOT

OUTPUT

Add a comment
Know the answer?
Add Answer to:
You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT