Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'int' object is unsubscriptable

Tags:

python

In python I get this error:

TypeError: 'int' object is unsubscriptable

This happens at the line:

sectorcalc[i][2]= ((today[2]/yesterday[2])-1)

I couldn't find a good definition of unsubscriptable for python anywhere.

for quote in sector[singlestock]:
        i+=1
        if i < len(sector):
            if i==0:
                sectorcalc[i][0]= quote[0]
                sectorcalc[i][2]= 0
                sectorcalc[i][3]= 0
                sectorcalc[i][4]= 0
                sectorcalc[i][5]= 0
                sectorcalc[i][6]= 0
                sectorcalc[i][7]= 0
            else:                    
                yesterday = sector[singlestock-1][i]

                print yesterday                                

                today = quote

                print type(today[2])
                sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
                sectorcalc[i][3]= (today[3]/yesterday[3])-1
                sectorcalc[i][4]= (today[4]/yesterday[4])-1
                sectorcalc[i][5]= (today[5]/yesterday[5])-1 
                sectorcalc[i][6]= (today[6]/yesterday[6])-1
                sectorcalc[i][7]= (today[7]/yesterday[7])-1

What does this error mean?

like image 551
b8b8j Avatar asked Oct 30 '10 20:10

b8b8j


1 Answers

The "[2]" in today[2] is called subscript.

This usage is possible only if "today" is a sequence type. Native sequence types - List, string, tuple etc

Since you are getting an error - 'int' object is unsubscriptable. It means that "today" is not a sequence but an int type object.

You will need to find / debug why "today" or "yesterday" is an int type object when you are expecting a sequence.

[Edit: to make it clear]

Error can be in

  1. sectorcalc[i]
  2. today (Already proved is a list)
  3. yesterday
like image 93
pyfunc Avatar answered Oct 20 '22 00:10

pyfunc