TypeError % 不支持操作数类型:浮点型和无类型
题
很抱歉用一个菜鸟问题打扰您,但我是 Python 新手。基本上,这是一项我无法理解我做错了什么的家庭作业。我想我已经拥有了我需要的一切,但我不断出现打字错误。任何帮助表示赞赏。谢谢!
def Main():
Weight = float(input ("How much does your package weigh? :"))
CalcShipping(Weight)
def CalcShipping(Weight):
if Weight>=2:
PricePerPound=1.10
elif Weight>=2 & Weight<6:
PricePerPound=2.20
elif Weight>=6 & Weight<10:
PricePerPound=float(3.70)
else:
PricePerPound=3.8
print ("The total shipping cost will be $%.2f") % (PricePerPound)
Main()
解决方案
这 print()
函数返回 None
;你可能想移动 %
手术 进入 函数调用:
print ("The total shipping cost will be $%.2f" % PricePerPound)
请注意,您的 if
测试正在使用 按位 和 操作员 &
;你可能想用 and
相反,使用布尔逻辑:
elif Weight >= 2 and Weight < 6:
PricePerPound = 2.20
elif Weight >= 6 and Weight < 10:
PricePerPound = 3.70
或者,使用比较链:
elif 2 <= Weight < 6:
PricePerPound = 2.20
elif 6 <= Weight < 10:
PricePerPound = 3.70
查看您的测试,您测试了 Weight >= 2
太早了;如果 Weight
介于 2 到 6 之间,您将匹配第一个 if
并完全忽略其他陈述。我想你想要:
PricePerPound = 1.10
if 2 <= Weight < 6:
PricePerPound = 2.20
elif 6 <= Weight < 10:
PricePerPound = 3.70
elif Weight >= 10:
PricePerPound = 3.8
例如价格为1.10,除非您的包裹重量为2个或更多,之后价格逐渐上涨。
不隶属于 StackOverflow