I am stuck with this question:
Write a function driving_cost() with input parameters miles_per_gallon, dollars_per_gallon, and miles_driven, that returns the dollar cost to drive those miles. All items are of type float. The function called with arguments (20.0, 3.1599, 50.0) returns 7.89975.
Define that function in a program whose inputs are the car's miles per gallon and the price of gas in dollars per gallon (both float). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your driving_cost() function three times.
Output each floating-point value with two digits after the decimal point, which can be achieved as follows: print(f'{your_value:.2f}')
Ex: If the input is:
20.0 3.1599 the output is:
1.58 7.90 63.20 Your program must define and call a function: def driving_cost(miles_per_gallon, dollars_per_gallon, miles_driven)
The question also gets evaluated using a direct call of the function like this: driving_cost(20.0,3.1599, 50.0)
which is the part that is broken.
Here is my code so far:
def driving_cost(miles_per_gallon, dollars_per_gallon, miles_driven):return dollars_per_gallon/miles_per_gallon*miles_drivenmiles_per_gallon=float(input())
dollars_per_gallon=float(input())miles=[10, 50, 400]
for i in miles:print(f'{driving_cost(miles_per_gallon, dollars_per_gallon, i):.2f}')if __name__ == '__main__':miles_driven=float(input());cost=driving_cost(miles_per_gallon, dollars_per_gallon, miles_driven);print(f'{cost:.2f}');
If I just input values, the top section executes but if I call the function like this driving_cost(20.0,3.1599, 50.0)
I get:
Traceback (most recent call last):File "main.py", line 4, in <module>miles_per_gallon=float(input())
ValueError: could not convert string to float: 'driving_cost(20.0, 3.1599, 50.0)'
I don't completely grasp the if __name__ == '__main__':
thing either but I really don't understand why this error is occurring. Even when I include the other inputs in the if __name__ == '__main__':
part of the program, I get that error. Help please?