简体   繁体   English

驱动成本 - python 中的函数 - 如果 __name__ == '__main__' 中的 EOFerror:

[英]Driving costs - functions in python- EOFerror in if __name__ == '__main__':

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.使用输入参数miles_per_gallon、dollar_per_gallon 和miles_driven 编写一个函数driving_cost(),它返回行驶这些英里的美元成本。 All items are of type float.所有项目都是浮动类型。 The function called with arguments (20.0, 3.1599, 50.0) returns 7.89975.使用参数 (20.0, 3.1599, 50.0) 调用的函数返回 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.通过调用您的driving_cost() 函数三次,输出 10 英里、50 英里和 400 英里的汽油成本。

Output each floating-point value with two digits after the decimal point, which can be achieved as follows: print(f'{your_value:.2f}')输出每个浮点值小数点后两位,可以实现如下:print(f'{your_value:.2f}')

Ex: If the input is:例如:如果输入是:

20.0 3.1599 the output is: 20.0 3.1599 输出为:

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) 1.58 7.90 63.20 你的程序必须定义并调用一个函数: defdriving_cost(miles_per_gallon, Dollar_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.该问题还可以通过直接调用如下函数进行评估: driving_cost(20.0,3.1599, 50.0)这是损坏的部分。

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_driven
    
miles_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:如果我只是输入值,则顶部会执行但如果我调用这样的函数driving_cost(20.0,3.1599, 50.0)我得到:

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.我也没有完全理解if __name__ == '__main__':事情,但我真的不明白为什么会发生这个错误。 Even when I include the other inputs in the if __name__ == '__main__': part of the program, I get that error.即使我在if __name__ == '__main__':程序的一部分中包含其他输入,我if __name__ == '__main__':收到该错误。 Help please?请帮忙?

The traceback is telling you the problem happens on line 4 and shows you the line that caused the problem:回溯告诉您问题发生在第 4 行,并向您显示导致问题的行:

miles_per_gallon=float(input())

Even though you probably added driving_cost(20.0,3.1599, 50.0) somewhere further down, you still have this line in your code and it still takes input, which still has to be a string representing a valid floating point number, or you will get that error.即使您可能在driving_cost(20.0,3.1599, 50.0)某个地方添加了driving_cost(20.0,3.1599, 50.0) ,您的代码中仍然有这一行并且它仍然需要输入,它仍然必须是一个表示有效浮点数的字符串,否则您会得到错误。

As for the if __name__ == '__main__': part: your script gets executed top to bottom when you run it.至于if __name__ == '__main__':部分:您的脚本在您运行时从上到下执行。 But if your script contains some useful functions you'd like to use from some other script, you may way to import my_script or from my_script import driving_cost .但是,如果您的脚本包含一些您想从其他脚本中使用的有用函数,您可以import my_scriptfrom my_script import driving_cost

However, whenever you import a module, Python actually runs the module.但是,无论何时import模块,Python 实际上都会运行该模块。 If all the code in the module lives inside functions, that's OK, nothing gets run until a function gets called.如果模块中的所有代码都存在于函数中,那没关系,在调用函数之前不会运行任何代码。 But your script does execute all sorts of code.但是您的脚本确实会执行各种代码。

That's where if __name__ == '__main__': comes in. Every script has a __name__ variable defined.这就是if __name__ == '__main__':进来的地方。每个脚本都定义了一个__name__变量。 If your script is the main script being run by Python, its __name__ will be '__main__' .如果您的脚本是 Python 运行的主脚本,则其__name__将为'__main__' So, that line basically says "if this script is being run as the main script, run some code, otherwise (if this script is being run as a module) don't".因此,该行基本上是说“如果此脚本作为主脚本运行,请运行一些代码,否则(如果此脚本作为模块运行)不要”。

As a result, this would be a better way to organise your script:因此,这将是组织脚本的更好方法:

def driving_cost(miles_per_gallon, dollars_per_gallon, miles_driven):
    return dollars_per_gallon / miles_per_gallon * miles_driven


def input_compute_and_print():
    miles_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}')


def print_example():
    cost = driving_cost(20.0,3.1599, 50.0)
    print(f'{cost:.2f}')


if __name__ == '__main__':
    input_compute_and_print()
    # print_example()

Note that this is just your own code reorganised (without the later changes).请注意,这只是您自己重新组织的代码(没有后来的更改)。 By commenting out the calls in the bottom section, you can get separate parts to run.通过注释掉底部的调用,您可以运行单独的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM