TypeError: 'int' object is not subscriptable

前端 未结 6 563
一生所求
一生所求 2020-12-13 09:08

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, i         


        
6条回答
  •  一生所求
    2020-12-13 09:44

    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
    

提交回复
热议问题