gpt4 book ai didi

python - 在这种情况下,我如何最好地解决函数比较 TypeError 问题?

转载 作者:行者123 更新时间:2023-12-04 08:15:38 26 4
gpt4 key购买 nike

我正在尝试制作一个程序,它可以根据包裹的重量自动确定邮寄包裹的最便宜的方法。
但是代码拒绝运行,因为我不断收到以下信息 TypeError: '<' not supported between instances of 'function' and 'function' .
我尝试将函数转换为浮点数:if float(cost_ground) < float(cost_drone) and float(cost_ground) < float(cost_premium):但这会返回以下 TypeError: float() argument must be a string or a number, not 'function' .
关于如何解决这个问题的任何建议和/或解释?
这是完整的代码:

def cheapest(weight):
def cost_ground(weight):
if weight <= 2:
return float((weight * 1.50) + 20)
elif weight <= 6:
return float((weight * 3.00) + 20)
elif weight <= 10:
return float((weight * 4.00) + 20)
else:
return float((weight * 4.75) + 20)
def cost_drone(weight):
if weight <= 2:
return float((weight * 4.50))
elif weight <= 6:
return float((weight * 9.00))
elif weight <= 10:
return float((weight * 12.00))
else:
return float((weight * 14.25))
def cost_premium(weight):
return 125.00
if cost_ground < cost_drone and cost_ground < cost_premium:
return "Ground shipping is the cheapest at $" + cost_ground
if cost_drone < cost_ground and cost_drone < cost_premium:
return "Drone shipping is the cheapest at $" + cost_drone
if cost_premium < cost_ground and cost_premium < cost_drone:
return "Premium ground shipping is the cheapest at $" + cost_premium

最佳答案

每个函数在调用时都需要给出它的参数。可以不带任何参数调用函数的唯一情况是函数本身不带参数。下面的代码应该清楚这一点:

def function_a():
print("No parameters required.")

def function_b(parameter_1,parameter_2):
return parameter_1 + parameter_2
function_a()
function_b()
不带任何参数调用第一个函数不会产生任何错误,但是调用第二个函数会产生错误,因为它需要两个参数并且没有给出任何参数。
在您的代码中,您没有将参数传递给函数。自己定义, cost_ground()需要一个 weigth范围。当您调用该函数时,您是在不带参数的情况下调用它。
因此它应该是:
if float(cost_ground(weight)) < float(cost_drone(weight)) and float(cost_ground(weight)) < float(cost_premium(weight)):
除此之外,当您返回值时,您必须转换来自 float 的值。至 string如下:
return "Ground shipping is the cheapest at $" + str(cost_ground(weight))
否则会报如下错误:
TypeError: can only concatenate str (not "float") to str
下面给出了完整的代码,现在应该可以正常工作了。
def cheapest(weight):
def cost_ground(weight):
if weight <= 2:
return float((weight * 1.50) + 20)
elif weight <= 6:
return float((weight * 3.00) + 20)
elif weight <= 10:
return float((weight * 4.00) + 20)
else:
return float((weight * 4.75) + 20)
def cost_drone(weight):
if weight <= 2:
return float((weight * 4.50))
elif weight <= 6:
return float((weight * 9.00))
elif weight <= 10:
return float((weight * 12.00))
else:
return float((weight * 14.25))
def cost_premium(weight):
return 125.00
if float(cost_ground(weight)) < float(cost_drone(weight)) and float(cost_ground(weight)) < float(cost_premium(weight)):
return "Ground shipping is the cheapest at $" + str(cost_ground(weight))
if cost_drone < cost_ground and cost_drone < cost_premium:
return "Drone shipping is the cheapest at $" + str(cost_drone(weight))
if cost_premium < cost_ground and cost_premium < cost_drone:
return "Premium ground shipping is the cheapest at $" + str(cost_premium(weight))

关于python - 在这种情况下,我如何最好地解决函数比较 TypeError 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65721244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com