Driving costs - functions in python- EOFerror in if __name__ == __main__:

2024/10/10 6:15:08

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?

Answer

This was a challenge, I ain't going to lie. I worked on this for hours and the instructions are sloppy to say the least. I put a default value on miles_driven as a parameter and then just did the math for 10, 50, and 400 miles in the print line at the bottom, calling the function (driving_cost) three times as instructed.

I tried to make a variable for a third input or argument (miles_driven), but it would not accept it for some reason. Not sure if it is a problem on there end because the instruction made it seem like a third argument would be sometimes inputted, but it never was.

The following code was accepted and marked as complete, so hope it helps.

def driving_cost(miles_per_gallon, dollars_per_gallon, miles_driven=10.0):return (miles_driven / miles_per_gallon) * dollars_per_gallonif __name__ == '__main__':# Type your code here.miles = float(input())dollars = float(input())price = driving_cost(miles, dollars) print(f'{price:.2f}')print(f'{price * 5:.2f}')print(f'{price * 40:.2f}')
https://en.xdnf.cn/q/118484.html

Related Q&A

Pandas loc dynamic conditional list

I have a Pandas DataFrame and I want to find all rows where the ith column values are 10 times greater than other columns. Here is an example of my DataFrame:For example, looking at column i=0, row B (…

no module named numpy python2.7

Im using python 2.7 on Linux CentOS 6.5. After successfully using yum to install numpy, I am unable to import the module.from numpy import *The above code produces the following error:no module named …

TypeError: list indices must be integers or slices, not tuple for list of tuples

I am getting "list indices must be integers or slices, not tuple" error while trying to generate list from list of tuples. list of tuples have the following structure:[(29208, 8, 8, 8), (2920…

Spark Unique pair in cartesian product

I have this:In [1]:a = sc.parallelize([a,b,c]) In [2]:a.cartesian(a).collect() Out[3]: [(a, a), (a, b), (a, c), (b, a), (c, a), (b, b), (b, c), (c, b), (c, c)]I want the following result:In [1]:a = sc.…

How to use double click bid manager(DBM) API in python

I am trying to use the google Double click bid manager (DBM) API, to download reports, I am trying to make this automatic without manual authentication, but all I can find is the GitHub repo for DBM sa…

How can I replace a value in an existing excel csv file using a python program?

How can I update a value in an existing .csv file using a python program. At the moment the file is read into the program but I need to be able to change this value using my program, and for the change…

Why might Python break down halfway through a loop? TypeError: __getitem__

The GoalI have a directory with 65 .txt files, which I am parsing, one by one, and saving the outputs into 65 corresponding .txt files. I then plan to concatenate them, but Im not sure if jumping strai…

RoboBrowser getting type error NoneType object is not subscriptable

Im trying to make a kahoot spammer which inputs a pin number and a username, decided by the user. Im getting a type error when I run this code:import re from robobrowser import RoboBrowser#Getting pin …

How to create a 2d list with all same values but can alter multiple elements within? (python)

Im trying to create a list that holds this exact format: [[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]]and when list[3][0] = 9 is called, the list becomes [[2],[9],[2],[9],[2],[9],[2],[9],[2],[9]]How do I c…

Understanding Function Closures [duplicate]

This question already has answers here:Why arent python nested functions called closures?(10 answers)Closed 9 years ago.Im struggling to understand Function closures properly. For example in the code …