Digit sum


#1

Could you guys help me with my code ?

I don't understand why it is wrong :"Oops, try again. Your function fails on digit_sum(434). It returns 0 when it should return 11."

Here's my code :

def digit_sum(n):
    n = []
    sumo=0
    str(n)
    for number in n:
        sumo += int(number)       
    return sumo

Digit_sum help - how to separated characters in a list
#2

here:

for number in n:

you loop over an empty list, why?


#3

def digit_sum(n):
    n = [str(n)]
    sumo=0
    for number in n:
        sumo += int(number)       
    return sumo

Better ? I struggle to understand how to get through this exercice


#4

what do you even need a list for? simply convert n to string (strings are iterable) and you should be good


#5

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