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