4.15 Digit Sum : I have no idea what's wrong with my code



This is my code:

def digit_sum(n) :
    str_n = str(n)
    lst_n = list(str_n)
    for i in lst_n:
        numbers= []
        integer = int(lst_n[i])
    return sum(numbers)

error message says:
Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "list indices must be integers, not str" error.

I thought I have successfully changed string in to list of integers, but the error messages says that I didn't.

I am so stuck. can anybody help me out with this?

Replace this line with your code.


if i was containing indexes (which is not the case), you would need to cast i to integer before you can access lst_n by index

but i contains values directly from list, not indexes

also, by placing numbers= [] inside the loop, you will get a new list every iteration of the loop, so it won't retain what you appended in the previous iteration


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.