简体   繁体   中英

How to fix “TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'”

I'm creating a program that calculates the weight on top of each person in a human pyramid, assuming each person conveniently weighs 200 pounds. My problem is the last 'elif' in my function, which brings up the error: TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'.

This needs to be a recursive function for my class.

I've tried a 'return' statement already and 'tot =' instead of 'tot +='.

tot = 0.0

def prac(r, c):

    global tot
    if c > r:
        print('Not valid')
    elif r == 0 and c >= 0:
        print(tot, 'lbs')
    elif r > 0 and c == 0:
        tot += (200 / (2 ** r))
        prac(r - 1, c)
    elif r > 0 and c == r:
        tot += (200 / (2 ** r))
        prac(r - 1, c - 1)
    elif r > 0 and r > c > 0:
        tot += (200 + (prac(r - 1, c - 1)) + (prac(r - 1, c)))
        prac(r == 0, c == 0)



prac(2, 1)

I expect it to calculate prac(2,1) to 300 lbs , prac(3,1) to 425, etc.

The prac function doesn't return anything, and functions that don't return are given the None type. In the last elif statement you are trying to add None to tot, which will raise the error you get.

I'm not sure what you code is trying to accomplish, so it's hard to post a correct answer, but here is a guess:

tot = 0.0

def prac(r, c):

    global tot
    if c > r:
        print('Not valid')
    elif r == 0 and c >= 0:
        print(tot, 'lbs')
    elif r > 0 and c == 0:
        tot += (200 / (2 ** r))
        prac(r - 1, c)
    elif r > 0 and c == r:
        tot += (200 / (2 ** r))
        prac(r - 1, c - 1)
    elif r > 0 and r > c > 0:
        x = prac(r - 1, c - 1)
        y = prac(r - 1, c)
        tot += 200
        if x is not None:
            tot += x
        if y is not None:
            tot += y
        prac(r == 0, c == 0)



prac(2, 1)

I went through your code and found out that you are not returning anything in your function which makes things bad in the last elif.

In each iteration, you are calling the function for further computation. Lets jump right into the last elif . Here, you are adding the values returned by the function along with the static value. Since you are not returning anything in the function, the value gets saved as NoneType . If you intend to terminate your loop at one else or elif, return the value from there. Then when you call the function in the last elif, the function will return something and the addition will proceed properly.

I don't know the mechanics but here is what I am trying to convey is make a stopping condition for the loop where it returns the value (you haven't catered the situation where C becomes less than 0 as well.

I hope you get my point. Best of luck!

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