TypeError: 'int' object is not subscriptable

Question:

I’m trying to create a simple program that tells you your lucky number according to numerology. I keep on getting this error:

File "number.py", line 12, in <module>
    sumln = (int(sumall[0])+int(sumall[1]))
TypeError: 'int' object is not subscriptable

My script is:

birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
summ = (int(birthday[0])+int(birthday[1]))
sumd = (int(birthday[3])+int(birthday[4]))
sumy= (int(birthday[6])+int(birthday[7])+int(birthday[8])+int(birthday[9]))
sumall = summ + sumd + sumy
print "The sum of your numbers is", sumall
sumln = (int(sumall[0])+int(sumall[1]))
print "Your lucky number is", sumln`   
Asked By: Cristal Isabel

||

Answers:

The error is exactly what it says it is; you’re trying to take sumall[0] when sumall is an int and that doesn’t make any sense. What do you believe sumall should be?

Answered By: Free Monica Cellio

You can’t do something like that: (int(sumall[0])+int(sumall[1]))

That’s because sumall is an int and not a list or dict.

So, summ + sumd will be you’re lucky number

Answered By: DonCallisto
sumall = summ + sumd + sumy

Your sumall is an integer. If you want the individual characters from it, convert it to a string first.

Answered By: kindall

Try this instead:

sumall = summ + sumd + sumy
print "The sum of your numbers is", sumall
sumall = str(sumall) # add this line
sumln = (int(sumall[0])+int(sumall[1]))
print "Your lucky number is", sumln

sumall is a number, and you can’t access its digits using the subscript notation (sumall[0], sumall[1]). For that to work, you’ll need to transform it back to a string.

Answered By: Óscar López

If you want to sum the digit of a number, one way to do it is using sum() + a generator expression:

sum(int(i) for i in str(155))

I modified a little your code using sum(), maybe you want to take a look at it:

birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
summ = sum(int(i) for i in birthday[0:2])
sumd = sum(int(i) for i in birthday[3:5])
sumy = sum(int(i) for i in birthday[6:10])
sumall = summ + sumd + sumy
print "The sum of your numbers is", sumall
sumln = sum(int(c) for c in str(sumall)))
print "Your lucky number is", sumln
Answered By: Rik Poggi

Just to be clear, all the answers so far are correct, but the reasoning behind them is not explained very well.

The sumall variable is not yet a string. Parentheticals will not convert to a string (e.g. summ = (int(birthday[0])+int(birthday[1])) still returns an integer. It looks like you most likely intended to type str((int(sumall[0])+int(sumall[1]))), but forgot to. The reason the str() function fixes everything is because it converts anything in it compatible to a string.

Answered By: Anonymous Person

this code works for summer_69. It looks too simple to be true 🙂

def summer_69(mylist):
    ignore = False
    sum = 0
    for i in mylist:
        if i == 6:
            ignore = True
        elif not ignore:
            sum = sum + i
        elif i == 9:
            ignore = False
        
    return sum

summer_69([1,2,2,6,3,7,9,3])
Answered By: suganya

I think the statement is self-explanatory.

[TypeError: ‘int’ object is not subscriptable]

You are trying to do something the computer can’t do. The data type "integer" cannot be subscripted. It should be a "string" to do that.

So, convert the integer data type to a string and you will be good to go. (Go back to the lessons on data types and subscription and come back.)

Keep up all the good work!!!

Answered By: Rajitha Amarasinghe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.