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

2024/10/9 6:25:24

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_examstotal_exams += 1
print(sum)sum = int("Please enter Exam grade, or press 999 to end: ")
while true:if sum <= 100:sum += total_examstotal_exams += 1elif 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 += totalExamstotalExams += 1
print(total_sum)exam_sum = int("Please enter Exam grade, or press 999 to end: ")
while true:if exam_sum <= 100:exam_sum += totalExamstotalExams += 1elif 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: '

Answer

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.

https://en.xdnf.cn/q/70051.html

Related Q&A

Project Scipy Voronoi diagram from 3d to 2d

I am trying to find a way to calculate a 2d Power Diagram in Python. For this I want to make use of the fact that a 2d power diagram can be interpreted as the intersection of a regular 3d voronoi diagr…

Where can I see the list of built-in wavelet functions that I can pass to scipy.signal.cwt?

scipy.signal.cwts documentation says:scipy.signal.cwt(data, wavelet, widths)wavelet : functionWavelet function, which should take 2 arguments. The first argument is the number of points that the return…

How to play sound in Python WITHOUT interrupting music/other sounds from playing

Im working on a timer in python which sounds a chime when the waiting time is over. I use the following code:from wave import open as wave_open from ossaudiodev import open as oss_opendef _play_chime()…

How can I use click to parse a string for arguments?

Say I have a list of strings containing arguments and options, with argparse, I’m able to parse this list using the parse_args function into an object, as follows:import argparseextra_params = [‘—su…

Tornado and WTForms

I am using WTForms for the first time. Using WTForms to validate POST requests in Tornado Below is my forms forms.pyclass UserForm(Form):user = TextField(user, [validators.Length(min=23, max=23)])In t…

Modifying a python docstring with a decorator: Is it a good idea?

A python docstring must be given as a literal string; but sometimes its useful to have similar docstrings for several functions (e.g., different constructors), or several access methods might accept th…

Counting number of columns in text file with Python

I have two text files composed of spaced-separated columns. These are excerpts of these two files:FileA1 1742.420 -0.410 20.1530 0.4190 1.7080 0.59402 1872.060 0.070 21.4710 0.2950 0.0…

Django on Apache web server dict object has no attribute render_context

Im having a bit of a problem, I uploaded my Django project to a webserver running apache, mod_python, and django. On the computer I developed on the following works finenameBox = getNamesBox().render(l…

Verbose log abbriviations meaning in SVC, scikit-learn

I am looking for the meaning of verbose log abbriviations of SVC function in scikit-learn?If nSV is the number of support vectors, #iter is the number of iteration, what dose nBSV, rho,obj mean?This …

networkx draw_networkx_edges capstyle

Does anyone know if it is possible to have fine-grained control over line properties when drawing networkx edges via (for example) draw_networkx_edges? I would like to control the line solid_capstyle …