1. Examples are as follows:
from math import sqrt
if __name__ == '__main__':
N = 100
a = range(0,N)
for i in range(2,int(sqrt(N))):
for j in range(i + 1,N):
if (a[i] != 0) and (a[j] != 0):
if a[j] % a[i] == 0:
a[j]= 0
for i in range(2,N):
if a[i] != 0:
print ("%5d" % a[i])
if (i - 2) % 10 == 0:
print
Error after execution: typeerror: ‘range’ object does not support item assignment
2. The reasons for the error are as follows:
Try to use range()
to create an integer list (leading to “typeerror: ‘range’ object does not support item assignment”). Sometimes you want to get an ordered integer list, so range() seems to be a good way to generate this list. However, you need to remember that range () returns the “range object” instead of the actual list value.
3. Solutions:
Just change the code of the above example: a = range (0, n) to a = list (range (0, n))!
Read More:
- TypeError: unsupported operand type(s) for *: ‘range‘ and ‘int‘
- Error: (22,57) Java: type parameter… Mapper is not in the range of type variable M
- Type error: sequence item 0: expected STR instance, int found
- Ie11 reports an error unhandled promise rejection typeerror: the object does not support
- Type error: object (…) is not a function
- Python error: local variable ‘XXXX’ referenced before assignment
- 【Bug-python】IndexError: list index out of range
- Type error at XXX ‘set’ object is not reversible
- Python UnboundLocalError: local variable ‘num’ referenced before assignment
- About content type ‘multipart / form data…’ not support
- Python 3 error typeerror: ‘dict’_ keys‘ object is not subscriptable
- [Solved] RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1‘
- Solution: attributeerror: type object ‘ioloop’ has no attribute ‘initialized’
- Python Numpy.ndarray ValueError:assignment destination is read-only
- Solving Python error: local variable ‘a’ referenced before assignment
- Python error: typeerror: ‘Int’ object is not subscribable
- Differences between shell script variable assignment and C language variable assignment
- Python custom class typeerror: ‘module’ object is not callable
- Python reported error: typeerror:’int’object is not subscriptable
- Type error: the JSON object must be STR, bytes or byte array, not ‘textiowrapper’