TypeError不支持%的操作数类型:Float和NoneTyp

2024-10-01 09:42:23 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉打扰你一个noob问题,但我是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()

Tags: inputyourmaindeffloathow家庭作业weight
1条回答
网友
1楼 · 发布于 2024-10-01 09:42:23

print()函数返回None;您可能希望将%操作移动到函数调用中:

print ("The total shipping cost will be $%.2f" % PricePerPound) 

请注意,if测试使用的是bitwise and operator ^{};您可能希望使用^{},使用布尔逻辑:

^{pr2}$

或者,使用比较链接:

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英镑或2英镑以上的包裹,否则价格会逐步上涨。在

相关问题 更多 >