简体   繁体   中英

TypeError unsupported Operand type(s) for %: Float and NoneType

sorry to bother you with a noob question, but I am new to Python. Basically this is a homework assignment that I cannot understand what I am doing wrong on. I think I have everything I need but I Keep getting a typeerror. Any help is appreciated. Thanks!

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()

The print() function returns None ; you probably wanted to move the % operation into the function call:

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

Note that your if tests are using the bitwise and operator & ; you probably wanted to use and instead, using boolean logic:

elif Weight >= 2 and  Weight < 6:
    PricePerPound = 2.20

elif Weight >= 6 and Weight < 10:
    PricePerPound = 3.70

or, using comparison chaining:

elif 2 <= Weight < 6:
    PricePerPound = 2.20

elif 6 <= Weight < 10:
    PricePerPound = 3.70

Looking over your tests, you test for Weight >= 2 too early; if Weight is between 2 and 6 you'll match the first if and ignore the other statements altogether. I think you wanted:

PricePerPound = 1.10

if 2 <= Weight < 6:
    PricePerPound = 2.20

elif 6 <= Weight < 10:
    PricePerPound = 3.70

elif Weight >= 10:
    PricePerPound = 3.8

eg the price is 1.10, unless you have a package weighing 2 or more, after which the price goes up progressively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM