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()
答案 0 :(得分:2)
print()
函数返回None
;您可能想将%
操作移动到函数调用中:
print ("The total shipping cost will be $%.2f" % PricePerPound)
请注意,您的if
测试正在使用bitwise and operator &
;你可能想用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
e.g。价格是1.10,除非您的包装重量为2或更多,之后价格会逐渐上涨。