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

时间:2014-02-25 18:08:40

标签: python typeerror nonetype

很抱歉打扰了一个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()

1 个答案:

答案 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或更多,之后价格会逐渐上涨。