219k views
3 votes
Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles.Output each floating-point value with two digits after the decimal point, which can be achieved as follows:printf("%0.2of", your Value);X: If the input is:20.0 3.1599the output is:1.58 7.90 63.20Note: Real per-mile cost would also include maintenance and depreciation.

1 Answer

5 votes

Answer:

#program in Python.

#function to calculate driving cost

def driving_cost(d_miles, m_per_gallon, doll_per_gallon):

#calculate cost and return the value

return (d_miles / m_per_gallon) * doll_per_gallon

#read the miles per gallon

m_per_gallon = float(input("Enter car's miles/gallon:"))

#read dollars per gallon

doll_per_gallon = float(input("Enter gas dollars/gallon:"))

#call function to find cost for 10 miles

print('%0.2f' % driving_cost(10, m_per_gallon, doll_per_gallon))

#call function to find cost for 50 miles

print('%0.2f' % driving_cost(50, m_per_gallon, doll_per_gallon))

#call function to find cost for 400 miles

print('%0.2f' % driving_cost(400, m_per_gallon, doll_per_gallon))

Step-by-step explanation:

Read the value of miles per gallon and assign it to variable "m_per_gallon". Then read dollars per gallon from user and assign it to variable "doll_per_gallon". Call the function driving_cost() with parameter miles, m_per_gallon and doll_per_gallon. This function will find the cost of driving and return the cost.Call function for 10, 50 and 400 miles.

Output:

Enter car's miles/gallon:20

Enter gas dollars/gallon:3.1599

1.58

7.90

63.20

User Ylisar
by
5.8k points