2014-02-25 136 views
0

对不起,打扰你一个noob问题,但我是Python的新手。基本上这是一项家庭作业,我不明白我做错了什么。我想我拥有我需要的所有东西,但我仍然得到一个类型错误。任何帮助表示赞赏。谢谢!TypeError不受支持的操作数类型为%:Float和NoneType

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

@MartijnPieters我认为你的意思是'按位'。 –

+0

@SilasRay你可能会忽略_not_。 – devnull

+0

我假设这是Python 3代码,对吧? – PearsonArtPhoto

回答

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 

例如价格是1.10,除非你有一个重量为2或以上的包裹,之后价格逐渐上涨。

相关问题