Python 类型错误:+= 不支持的操作数类型:'builtin_function_or_method' 和 'int'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19061250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:40:42  来源:igfitidea点击:

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

python

提问by user2825185

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

TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'尝试运行此代码时收到此错误 ( )

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.

总的来说,我只需要运行程序直到输入 999,然后找到所有输入数字的平均值。至少有一点帮助会很好。

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

文件“C:/Python33/vfvfv.py”,第 14 行,exam_sum = int("请输入考试成绩,或按 999 结束:") ValueError: 以 10 为基数的 int() 的无效文字:'请输入考试评分,或按 999 结束:'

回答by tamasgal

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

这是您的一个问题的答案,但是它不会对您有太大帮助,因为您的代码已经很糟糕了……

sumis a built-in function, just like lenfor example. Use another name and you're fine ;-)

sum是一个内置函数,就像len例如。使用另一个名字就可以了 ;-)

Further explanation:

进一步解释:

In this line

在这一行

sum += totalExams

you're doing

你正在做的

sum = sum + totalExams

where totalExamshas type intand sumis a built-in function in python. Since the +operator is not implemented for intand built-in-function, you get a TypeError. (sumwas not redefined before, so it's pointing to the function.)

wheretotalExams有类型int并且sum是 python 中的内置函数。由于+没有为intand实现运算符built-in-function,因此您会得到一个TypeError. (sum之前没有重新定义,所以它指向函数。)

You can solve it by simply choosing a variable name which is not already used, like total_sumor sum_examsetc.:

您可以通过简单地选择一个尚未使用的变量名称来解决它,例如total_sumsum_exams等等:

sum_exams += totalExams

Or simply declare it before you use it:

或者在使用之前简单地声明它:

sum = 0

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

警告:这样做,您将覆盖内置函数sum()

More problems:

更多问题:

Here, you're casting a stringto an int, which absolutely does not make a sense:

在这里,您将 a 转换string为 an int,这绝对没有意义:

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

我猜你想从用户那里获取一些输入并将其转换为整数?在这种情况下,您应该使用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

Trueis what you want…

True是你想要的……

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.

在所有这些修复之后,您最终会陷入无限循环。现在坐下来思考你的代码,然后再问下一个问题。

回答by cjfro

You have not defined sumbefore your first loop. If you add

您尚未sum在第一个循环之前定义。如果添加

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.

但是,您可能希望使用不同的变量名称,这样就不会覆盖内置函数。

回答by user2682768

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

如果您试图让用户输入数字,您可能指的是 int(input(...))