Question

In: Computer Science

Driving costs - functions Driving is expensive. Write a program with a car's miles/gallon and gas...

Driving costs - functions

Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon
(both floats) 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:
print('{:.2f}'.format(your_value))

Ex: If the input is:

20.0
3.1599
the output is:

1.58
7.90
63.20

Your program must define and call the following driving_cost() function.
Given input parameters driven_miles, miles_per_gallon, and dollars_per_gallon,
the function returns the dollar cost to drive those miles.

Use the following .py file to get started:

#variables

miles_per_gallon = float(input("Please enter the miles per gallon: "))
dollars_per_gallon = float(input("PLease enter the cost per gallon: "))

#calculations

dollars_20_miles = 20 * (1.0 / miles_per_gallon) * dollars_per_gallon
dollars_75_miles = 75 * (1.0 / miles_per_gallon) * dollars_per_gallon
dollars_500_miles = 500 * (1.0 / miles_per_gallon) * dollars_per_gallon

#print

print('{:.2f} {:.2f} {:.2f}'.format(dollars_20_miles, dollars_75_miles. dollars_500_miles))

Solutions

Expert Solution

Program:

def driving_cost(miles_per_gallon,dollars_per_gallon,driven_miles):
#calculations
#calculating cost for given driven_miles miles
return driven_miles * (1.0 / miles_per_gallon) * dollars_per_gallon

#input variables
miles_per_gallon = float(input("Please enter the miles per gallon: "))
dollars_per_gallon = float(input("PLease enter the cost per gallon: "))

#calling driving_cost() function with 10 driven_miles
driven_miles=10
c1 = driving_cost(miles_per_gallon,dollars_per_gallon,driven_miles)
print('{:.2f}'.format(c1))
#calling driving_cost() function with 50 driven_miles
driven_miles=50
c2=driving_cost(miles_per_gallon,dollars_per_gallon,driven_miles)
print('{:.2f}'.format(c2))
#calling driving_cost() function with 400 driven_miles
driven_miles=400
c3=driving_cost(miles_per_gallon,dollars_per_gallon,driven_miles)
print('{:.2f}'.format(c3))

output:

Another way to get solution:

PROGRAM:
def driving_cost(miles_per_gallon,dollars_per_gallon):
#calculations
dollars_10_miles = 10 * (1.0 / miles_per_gallon) * dollars_per_gallon
dollars_50_miles = 50 * (1.0 / miles_per_gallon) * dollars_per_gallon
dollars_400_miles = 400 * (1.0 / miles_per_gallon) * dollars_per_gallon
print('{:.2f}\n{:.2f}\n{:.2f}'.format(dollars_10_miles, dollars_50_miles, dollars_400_miles))

#input variables
miles_per_gallon = float(input("Please enter the miles per gallon: "))
dollars_per_gallon = float(input("PLease enter the cost per gallon: "))

#calling driving_cost() function with arguments
driving_cost(miles_per_gallon,dollars_per_gallon)

OUTPUT:


Related Solutions

Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If...
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If Congress mandates that cars have to get 36 miles per gallon, by what percentage will this lower the costs of driving? If the elasticity of total miles driven (per year) with respect to the cost of driving is –1, by how much will total miles driven per year increase, assuming it is 10,000 miles at the beginning? How much will total annual gas consumption...
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If...
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If Congress mandates that cars have to get 36 miles per gallon, by what percentage will this lower the costs of driving? If the elasticity of total miles driven (per year) with respect to the cost of driving is –1, by how much will total miles driven per year increase, assuming it is 10,000 miles at the beginning? How much will total annual gas consumption...
Write a beginner's program in Python to determine your car's gas mileage. You should use the...
Write a beginner's program in Python to determine your car's gas mileage. You should use the input variables "miles_traveled" and "gallons_fuel" and the result should be stored in the variable "miles_per_gallon".
A department of transportation's study on driving speeds and miles per gallon for midsize automobiles resulted...
A department of transportation's study on driving speeds and miles per gallon for midsize automobiles resulted in the following data: Speed (MPH) Miles per Gallon 30 28 50 25 40 25 55 23 30 30 25 32 60 21 25 35 50 26 55 25 (a) Find the line of best fit (b) Predict the mileage for a driving speed of 42 mph. c) Compute and interprent the correlation coefficient. (b) Is the relationship between speed and gas mileage statistically...
Forty years ago, a gallon of gas cost $1.20. Today, a gallon of gas costs $2.60....
Forty years ago, a gallon of gas cost $1.20. Today, a gallon of gas costs $2.60. Suppose that the gas price increase has been entirely due to inflation. a. Calculate the annual inflation rate. b. Today, you plan to make a cash purchase for a new car. The Model K costs $24,000 and you estimate the car will last 10 years and require 600 gallons per year. The Model M costs $28,000 but gets better mileage, so it will only...
Under specified driving conditions, an automobile manufacturer is concerned about the miles per gallon (mpg) of...
Under specified driving conditions, an automobile manufacturer is concerned about the miles per gallon (mpg) of its new crossover. For automobiles of the same class, the average is 21 with a standard deviation of 3.1 mpg. To investigate, the manufacturer tested 18 of its new crossover in which the average was 20.3 mpg. What can be concluded with α = 0.10? a) What is the appropriate test statistic? ---Select---na, z-test, one-sample t-test, independent-samples t-test, related-samples t-test b) Population: ---Select---specified conditions,...
The mean gas mileage for a hybrid car is 57 miles per gallon. Suppose that the...
The mean gas mileage for a hybrid car is 57 miles per gallon. Suppose that the gasoline mileage is approximately normally distributed with a standard deviation of 3.5 miles per gallon. ​ (a) What proportion of hybrids gets over 61 miles per​ gallon? (b) What proportion of hybrids gets 52 miles per gallon or​ less? (c) What proportion of hybrids gets between 59 and 62 miles per​ gallon? (d) What is the probability that a randomly selected hybrid gets less...
The mean gas mileage for a hybrid car is 56 miles per gallon. Suppose that the...
The mean gas mileage for a hybrid car is 56 miles per gallon. Suppose that the gasoline mileage is approximately normally distributed with a standard deviation of 3.2 miles per gallon.​ (a) What proportion of hybrids gets over 61 miles per​ gallon? (b) What proportion of hybrids gets 51 miles per gallon or​ less? left parenthesis c right parenthesis What proportion of hybrids gets between 59 and 61 miles per​ gallon? (d) What is the probability that a randomly selected...
The average gas mileage of a certain model car is 28.0 miles per gallon. If the...
The average gas mileage of a certain model car is 28.0 miles per gallon. If the gas mileages are normally distributed with a standard deviation of 0.4 miles per gallon, find the probability that a car has a gas mileage of between 26.8 and 27.2 miles per gallon.   In addition to the answer, please write out your steps and thoughts that led you to your answer.
Currently, a manager is using a regression model that predicts gas mileage​ (in miles per​ gallon)...
Currently, a manager is using a regression model that predicts gas mileage​ (in miles per​ gallon) based on the horsepower of a car and the​ car's weight​ (in pounds). She believes that the effect of HP on gas milage is affected by the weight and vice versa. Develop a regression model that includes​ horsepower, weight, and this new interaction effect to predict gas mileage. Complete parts​ (a) and​ (b). MPG- 15.3,19.5,20.8,18.9,17.2,27.5,44.4,27.2,28.3,21.1 Horsepower-187,101,143,171,166,72,70,89,87,134 Weight-4757,3537,3227,4448,4296,3191,2106,2490,2605,3871 a. At the 0.01level of​ significance, is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT