TypeError: 'int' object is not subscriptable

PythonPython 2.7

Python Problem Overview


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`   

Python Solutions


Solution 1 - Python

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?

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

sumall = summ + sumd + sumy

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

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

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])

Solution 8 - Python

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!!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCristal IsabelView Question on Stackoverflow
Solution 1 - PythonFree Monica CellioView Answer on Stackoverflow
Solution 2 - PythonRik PoggiView Answer on Stackoverflow
Solution 3 - PythonAnonymous PersonView Answer on Stackoverflow
Solution 4 - PythonkindallView Answer on Stackoverflow
Solution 5 - PythonDonCallistoView Answer on Stackoverflow
Solution 6 - PythonÓscar LópezView Answer on Stackoverflow
Solution 7 - PythonsuganyaView Answer on Stackoverflow
Solution 8 - PythonRajitha AmarasingheView Answer on Stackoverflow