user2825185
user2825185

Reputation: 121

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

I am receiving this error (TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int') when trying to run this code

total_exams = 0
for total_exams in range(1, 100001):
    sum += total_exams
print(sum)

sum = 0
total_exams = 0
while count <= 100000:
    sum += total_exams
    total_exams += 1
print(sum)

sum = int("Please enter Exam grade, or press 999 to end: ")
while true:
    if sum <= 100:
        sum += total_exams
        total_exams += 1
    elif sum == "999":
        print(sum / total_exams)

over all I just need to run the program until 999 is entered, and then find the average of all the numbers entered. At least a little help will be nice.

So i have edited my code to (new)

totalExams = 0
total_sum = 0
for totalExams in range (1, 100001):
    total_sum += totalExams
print(total_sum)

total_sum = 0
totalExams = 0
while totalExams <= 100000:
    total_sum += totalExams
    totalExams += 1
print(total_sum)

exam_sum = int("Please enter Exam grade, or press 999 to end: ")
while true:
    if exam_sum <= 100:
        exam_sum += totalExams
        totalExams += 1
    elif exam_sum == "999":
        print(exam_sum / totalExams)

Traceback (most recent call last):

File "C:/Python33/vfvfv.py", line 14, in exam_sum = int("Please enter Exam grade, or press 999 to end: ") ValueError: invalid literal for int() with base 10: 'Please enter Exam grade, or press 999 to end: '

Upvotes: 7

Views: 140568

Answers (3)

user2682768
user2682768

Reputation: 98

If you are trying to get the user to enter the number, you might have meant int(input(...))

Upvotes: -1

tamasgal
tamasgal

Reputation: 26269

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.

Upvotes: 12

cjfro
cjfro

Reputation: 222

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.

Upvotes: -1

Related Questions