close

[Solved] TypeError: unsupported operand type(s) for +=: ‘builtin_function_or_method’ and ‘int’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: unsupported operand type(s) for +=: ‘builtin_function_or_method’ and ‘int’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: unsupported operand type(s) for +=: ‘builtin_function_or_method’ and ‘int’ Error Occurs?

Today I get the following error TypeError: unsupported operand type(s) for +=: ‘builtin_function_or_method’ and ‘int’ in python.

How To Solve TypeError: unsupported operand type(s) for +=: ‘builtin_function_or_method’ and ‘int’ Error ?

  1. How To Solve TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int' Error ?

    To Solve TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int' Error You have not defined sum before your first loop. If you add

  2. TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'

    To Solve TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int' Error You have not defined sum before your first loop. If you add

Solution 1


Here is an answer to one of you problems, however it won’t help you that much, since your code is quite broken…

sum is a built-in function, just like len for example. Use another name and you’re fine 😉

Further explanation:

In this line

sum += totalExams

you’re doing

sum = sum + totalExams

where totalExams has type int and sum is a built-in function in python. Since the + operator is not implemented for int and built-in-function, you get a TypeError. (sum was not redefined before, so it’s pointing to the function.)

You can solve it by simply choosing a variable name which is not already used, like total_sum or sum_exams etc.:

sum_exams += totalExams

Or simply declare it before you use it:

sum = 0

Caveat: doing so, you’ll overwrite the built-in function sum().

More problems:

Here, you’re casting a string to an int, which absolutely does not make a sense:

exam_sum = int("Please enter Exam grade, or press 999 to end: ")

I guess you’re trying to get some input from the user and cast it to an integer? In this case, you should use input():

exam_sum = input("Please enter Exam grade, or press 999 to end: ")

And before you edit your question again, the next error will be

NameError: name 'true' is not defined

True is what you want…

Last but not least

After all these fixes you’ll end up with an infinite loop. Now sit back and think about your code before asking the next question.

Solution 2

You have not defined sum before your first loop. If you add

sum = 0

it will work fine.

However, you probably want to use a different variable name so you are not overriding the built in function.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read