在我的课堂上,我们正在学习如何使用函数,我永远也找不出我在这里做错了什么。我们必须计算一次旅行后的汽油费用,虽然我的程序还没有完成,但这就是我到目前为止所拥有的。
MPG_HIGHWAY = 39
MPG_CITY = 28
GALLON = 2.29
def main():
total_miles = float(input('Enter the total number of miles driven: '))
highway_percentage = float(input('Enter your percentage of time on the highway: '))
gas_expense()
def total_highway_miles(total_miles, highway_percentage):
total_highway_miles = total_miles * highway_percentage
return total_highway_miles
def total_city_miles(total_miles, total_highway_miles):
total_city_miles = total_miles - total_highway_miles
return total_city_miles
def gas_expense(total_highway_miles, total_city_miles):
total_miles = (total_highway_miles / MPG_HIGHWAY) + (total_city_miles / MPG_CITY)
gas_expense = total_miles * GALLON
main()我似乎不能正确地调用变量,这样它就可以在用户输入之后计算信息。不知道我在哪里出错了,但如果能帮我指明正确的方向,那就太好了。谢谢!
发布于 2017-10-11 08:32:44
您的错误消息应该会告诉您哪里出了问题。在运行代码并提供一些输入后,您将获得
回溯(最近一次调用):主TypeError中文件"“的第1行,文件"”的第4行: gas_expense()恰好接受2个参数(给定0)
因此,查看引用的代码,我们会发现:
gas_expense()作为函数调用,但实际的函数签名是
def gas_expense(total_highway_miles, total_city_miles):您需要将这些值传递给函数以使其保持良好状态。
发布于 2017-10-11 12:17:07
希望这能有所帮助。所以,首先你有这些常量:
MPG_HIGHWAY = 39
MPG_CITY = 28
GALLON = 2.29这些常量可以在所有函数中使用。但仅在gas_expense中使用。
您希望通过调用函数main()来计算燃气费
def main():
total_miles = float(input('Enter the total number of miles driven: '))
highway_percentage = float(input('Enter your percentage of time on the highway: '))
gas_expense()在这里,您没有在上面的函数中使用total_miles和highway_percentage的值,您只需要输入值。此外,gas_expense()还会产生一个错误,因为您已经将其定义为:
def gas_expense(total_highway_miles, total_city_miles):
total_miles = (total_highway_miles / MPG_HIGHWAY) + (total_city_miles / MPG_CITY)
gas_expense = total_miles * GALLON因此,函数gas_expense需要两个输入。例如:gas_expense(100, 200)不会产生错误。
此外,x = gas_expense(100, 200)还会产生一个错误。因为这只会做(计算)你在函数中告诉的事情,而不会返回任何值。返回值的步骤:
def gas_expense(total_highway_miles, total_city_miles):
total_miles = (total_highway_miles / MPG_HIGHWAY) + (total_city_miles / MPG_CITY)
gas_expense = total_miles * GALLON
return gas_expense例如:x = gas_expense(100, 200)将导致x的值为22.2289377。
但是..我可能建议函数和变量不要使用相同的名称。
使用其他两个函数:total_highway_miles和total_city_miles没有任何问题。要获得气体费,主要函数可以是:
def main():
total_miles = float(input('Enter the total number of miles driven: '));
highway_percentage = float(input('Enter your percentage of time on the highway: '));
x = total_highway_miles(total_miles, highway_percentage);
y = total_city_miles(total_miles, x);
return gas_expense(x, y)发布于 2017-10-11 09:22:58
下面的代码将帮助您了解代码中的错误所在。
MPG_HIGHWAY = 39
MPG_CITY = 28
GALLON = 2.29
def main():
total_miles = float(input('Enter the total number of miles driven: '))
highway_percentage = float(input('Enter your percentage of time on the highway: '))
thm = total_highway_miles(total_miles, highway_percentage)
tvm = total_city_miles(total_miles, thm)
gas_expense(thm, tvm)
def total_highway_miles(total_miles, highway_percentage):
return total_miles * highway_percentage
def total_city_miles(total_miles, total_highway_miles):
return total_miles - total_highway_miles
def gas_expense(total_highway_miles, total_city_miles):
total_miles = (total_highway_miles / MPG_HIGHWAY) + (total_city_miles / MPG_CITY)
gas_expense = total_miles * GALLON
main()https://stackoverflow.com/questions/46677909
复制相似问题