3 digit sum


#1



Oops try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "string indices must be integers, not str" error.
What's the problem here?


def digit_sum(n):
     lst = []
     a = str(n)
     lst.append(a)
     for i in lst:
         a[i] = int(a[i])
     return sum(lst)


#2

an example for [4,5,4]

this line:

lst.append(a)

this will result in:

['[4, 5, 4]']

you just append a string to list, so then looping over lst, you get:

[4,5,4]

then you try a list to access by index?


#3

Doing a for-each loop will set the variable i to the value of the element in the lst list (set to string on the line a = str(n)). Accessing strings and lists via indexes require integers, unlike dictionaries, so it raises this error.


#4

Okay, I passed! Thanks for the help!


#5