Driving is expensive. Write a program with a car's miles/gallon (as float), gas dollars/gallon (as float), the number of miles to drive (as int) as input, compute the cost for the trip, and output the cost for the trip. Miles/gallon, dollars/gallon, and cost are to be printed using two decimal places. Note: if milespergallon, dollarspergallon, milestodrive, and trip_cost are the variables in the program then the output can be achieved using the print statement: print('Cost to drive {:d} miles at {:f} mpg at $ {:.2f}/gallon is: $ {:.2f}'.format(milestodrive, milespergallon, dollarspergallon, trip_cost)) Ex: If the input is: 21.34 3.15

Respuesta :

Answer:

milespergallon = float(input())

dollarspergallon = float(input())

milestodrive = int(input())

trip_cost = milestodrive / milespergallon * dollarspergallon

print('Cost to drive {:d} miles at {:f} mpg at $ {:.2f}/gallon is: $ {:.2f}'.format(milestodrive, milespergallon, dollarspergallon, trip_cost))

Explanation:

Get the inputs from the user for milespergallon, dollarspergallon and milestodrive

Calculate the trip_cost, divide the milestodrive by milespergallon to get the amount of gallons used. Then, multiply the result by dollarspergallon.

Print the result as requested in the question

Answer:

def driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon):

  gallon_used = driven_miles / miles_per_gallon

  cost = gallon_used * dollars_per_gallon  

  return cost  

miles_per_gallon = float(input(""))

dollars_per_gallon = float(input(""))

cost1 = driving_cost(10, miles_per_gallon, dollars_per_gallon)

cost2 = driving_cost(50, miles_per_gallon, dollars_per_gallon)

cost3 = driving_cost(400, miles_per_gallon, dollars_per_gallon)

print("%.2f" % cost1)

print("%.2f" % cost2)

print("%.2f" % cost3)

Explanation: