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


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#

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])
        numbers.append(integer)
    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.


#2

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


#4

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