Python Math - TypeError: Object "NoneType" cannot be decoded

I am doing a little math program (no particular reason, just wanted to) and I ran into the "TypeError: Object" NoneType "cannot be decoded.

I've never seen this error before, so I have no idea what it means.

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista = list.sort(lista)

a = lista[1] - lista[0]

list = [v2, v4]
list = list.sort(list)

b = list[1] = list[0]

print str(a)+str("a")+str(" = ")+str(b)

      

Mistake:

Traceback (most recent call last):
  File "C:/Users/Nathan/Documents/Python/New thing", line 16, in <module>
    a = lista[1] - lista[0]
TypeError: 'NoneType' object is not subscriptable

      

+24


source to share


4 answers


lista = list.sort(lista)

      

It should be

lista.sort()

      

The method .sort()

is in place and returns None. If you want something out of place that returns a value, you can use

sorted_list = sorted(lista)

      

Beyond # 1: please don't call your lists list

. These are clobbers built-in list type.



Other than # 2: I'm not sure what this line is supposed to do:

print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

      

it's simple

print "value 1a + value 2 = value 3a value 4"

      

? In other words, I don't know why you are calling str on things that are already str.

Beyond # 3: sometimes you use print("something")

(Python 3 syntax) and sometimes you use print "something"

(Python 2). The latter will give you a SyntaxError in py3, so you must run 2. *, in which case you probably don't want to get in the habit, or you end up printing tuples, with extra parentheses. I admit that it will work well enough because if there is only one element in the parentheses, it is not interpreted as a tuple, but it looks odd to the pythonic eye.

+30


source


The exception TypeError: 'NoneType' object is not subscriptable

happens because the value lista

actually is None

. You can reproduce TypeError

what you get in your code if you try this on the Python command line:

None[0]

      

The reason it is lista

set to None is because the return value list.sort()

is None

... it does not return a sorted copy of the original list. Instead, as pointed out in, the list is sorted in place instead of the copy made (this is for efficiency reasons).



If you don't want to change the original version, you can use

other_list = sorted(lista)

      

+21


source


From this link https://docs.python.org/2/tutorial/datastructures.html you can read this method "Sorting list items in place", this means that the result value will be sorted and the result will be included itself. The function returns None.

When you assign the result to "lista" on line 14

lista = list.sort(lista)

      

You set it to None. This is mistake. None always has data and cannot be subscribed. "TypeError: Object 'NoneType' could not be signed"

to fix this error (for sorting the list), do this on line 14:

lista.sort() # this will sort the list in line

      

But there are other errors too: on line 18 when assigning:

list = [v2, v4]

      

You close this built-in list type and you get the following error:

TypeError: 'list' object is not callable

      

To fix this, say:

lista2 = [v2, v4]

      

Again on line 19, the same error on line 14. Do this to sort another list:

lista2.sort()

      

On line 21, you are trying to index the built-in list of types. To fix this:

b = lista2[1] = lista2[0]

      

With this, your code will work fine. Finally all the correct code:

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista.sort()

a = lista[1] - lista[0]

lista2 = [v2, v4]
lista2.sort()

b = lista2[1] = lista2[0]

print str(a)+str("a")+str(" = ")+str(b)

      

0


source


As stated earlier in one of the answers, this error occurs when the list value turns out to be empty. While not entirely relevant to this question, I got the same error when reading images using opencv and numpy because the filename was found to be different from the one specified, probably or because the working directory was not specified correctly.

0


source







All Articles