开发者

TypeError: 'int' object is unsubscriptable

In python I get this error:

TypeError: 'int' object is开发者_如何学Go 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?


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


This is confusing to read:

today = quote 

Is today = datetime.date.today()? Why would a date suddenly refer to a quote? Should the variable name be quoteForToday or something more expressive? Same for yesterday. Dividing two dates as you do makes no sense to me.

Since this is a quote, would today and yesterday refer to prices or rates on different days? Names matter - choose them carefully. You might be the one who has to maintain this six months from now, and you won't remember what they mean, either.

Not that the code you wrote is valid, but I can't see why you wouldn't use a loop.

for j in range(2,7):
    sectorcalc[i][j] = (today[j]/yesteday[j])-1

instead of

        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 


How to reproduce that error:

myint = 57
print myint[0]  

The people who wrote the compiler said you can't do that in the following way:

TypeError: 'int' object is unsubscriptable

If you want to subscript something, use an array like this:

myint = [ 57, 25 ]
print myint[0]

Which prints:

57

Solution:

Either promote your int to a list or some other indexed type, or stop subscripting your int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜