DEV Community

Discussion on: TypeError: 'int' object is not subscriptable

Collapse
 
kip13 profile image
kip • Edited

Check this code:

x = my_request["jsonObject"]["items"]

for ex in range(x):
    print(ex)

In the screenshot you have a an array of items right ? Is something like this:

>>> x = [{'a': 1, 'b': 2}]
>>> print(range(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object cannot be interpreted as an integer

range only accept an integer or other object that can be interpreted like one.

Maybe the line when you get the items is wrong, you got the number of items not the items, how are you parsing the JSON from the request ?

Collapse
 
crhodes2 profile image
crhodes

I parse my json as a array or as a dictionary. Not really confident with my answer but I will confirm my findings once I get in front of my workload on my computer

Collapse
 
crhodes2 profile image
crhodes • Edited

I figured out what the problem is. I had assumed that my variable x was supposed to be my object, but what it really was, was an integer.
I went back to the drawing board to locate the actual object and got it to display what I wanted.
So... problem solved!

Collapse
 
kip13 profile image
kip

I said that in the below comment:

range only accept an integer or other object that can be interpreted like one.
Maybe the line when you get the items [x = my_request["jsonObject"]["items"]
] is wrong, you got the number of items not the items.

Good for you @crhodes, remember always read the doc about functions that you use.

Collapse
 
crhodes2 profile image
crhodes • Edited

Here's a better screenshot. It should make things clear:
From line 1 to line 36, that is just one item. The a, b, c... are attribute related to the item. In fact, b was changed to itemInfo and inside itemInfo are attributes like form, shape, and color, and everything from url to payload are inside itemInfo
dropbox.com/s/w2jsmja0b83kky6/Scre...