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.

Respuesta :

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

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

fichoh

The program which finds the cost in dollar based on the miles per gallon and the number of miles driven is written in python 3 thus :

def cost(miles, mpg, dpg):

#initialize a function called cost which takes in 3 parameters

dollar_cost = (miles/mpg)*dpg

#the dollar cost variable calculated the dollar amount required to drive any given distance

print("{:.2f}".format(dollar_cost ))

#displays the dollar cost formated to two decimal places.

mpg = eval(input())

dpg = eval(input())

#takes input from the user for miles per gallon and cost pa’er gallon of gas

#sample runs of the function for 10 miles, 50 miles and 400 miles

cost(10, mpg, dpg)

cost(50, mpg, dpg)

cost(400, mpg, dpg)

Learn more: https://brainly.com/question/14276852

Ver imagen fichoh
Q&A Education