3 digit sum


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)
     for i in lst:
         a[i] = int(a[i])
     return sum(lst)


an example for [4,5,4]

this line:


this will result in:

['[4, 5, 4]']

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


then you try a list to access by index?


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.


Okay, I passed! Thanks for the help!