SoFunction
Updated on 2024-11-13

pythonreport an errorTypeError: ‘NoneType‘ object is not subscriptableThe solution to the problem

Problems found

I got this error while writing python, and then the solution from online tutorials was almost always - "redefine this variable", which made me look confused!

Then I realized that I had assigned the return None method to a variable, and then manipulated the variable to cause the problem.

  for i in range(2000):
    read_lines = (read_lines) # That's the problem
    print(read_lines)

It doesn't seem to be a problem, but when you run it, it reports an error

>>TypeError: 'NoneType' object is not subscriptable

Later, I found out that this function returns None, but I assigned it to read_lines, which led to this error in subsequent operations of read_lines, including printing read_lines also reported an error.

This is the code from the random library (see the comment that says return None).

  def shuffle(self, x, random=None):
    """
    Shuffle list x in place, and return None.

    Optional argument random is a 0-argument function returning a
    random float in [0.0, 1.0); if it is the default None, the
    standard  will be used.

    """

    if random is None:
      randbelow = self._randbelow
      for i in reversed(range(1, len(x))):
        # pick an element in x[:i+1] with which to exchange x[i]
        j = randbelow(i+1)
        x[i], x[j] = x[j], x[i]
    else:
      _int = int
      for i in reversed(range(1, len(x))):
        # pick an element in x[:i+1] with which to exchange x[i]
        j = _int(random() * (i+1))
        x[i], x[j] = x[j], x[i]

prescription

Just change the assignment statement in the above line.

  for i in range(2000):
    (read_lines)
    print(read_lines)
    content_list = []

summarize

To this point this article on python error TypeError: 'NoneType' object is not subscriptable solution to this article, more related python error TypeError solution content please search my previous posts! or continue to browse the following related articles I hope you will support me more in the future!