Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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: '

like image 753
user2825185 Avatar asked Sep 27 '13 22:09

user2825185


People also ask

How do you fix unsupported operand type S for NoneType and int?

The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

How do you fix unsupported operand types?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .

How do you fix unsupported operand type S for STR and STR?

The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .

What does unsupported operand type S for +: int and list?

The Python "TypeError: unsupported operand type(s) for +: 'int' and 'list'" occurs when we try to use the addition (+) operator with a number and a list. To solve the error, figure out where the variable got assigned a list and correct the assignment, or access a specific value in the list.


1 Answers

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.

like image 138
tamasgal Avatar answered Nov 15 '22 11:11

tamasgal