This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Chris01178
over 8 years

Practice Makes Perfect - digit_sum 4/15

Can someone explain why this does not work?

def digit_sum(n):
    count = 0
    for i in n:
        if i >= 0:
            return count + i
        else:
            return "Positive numbers only"

Answer 53fe8a888c1ccc3b820004e5

4 votes

Permalink

Also solved:

def digit_sum(n):
    total = 0
    for char in str(n):
        total += int(char)
    return total
points
Submitted by DaveHarbour
over 8 years

Answer 53fda379282ae3bcce0014e9

0 votes

Permalink

Ok Solved:

def digit_sum(n):
    str_num = str(n)
    lst = list(str_num)
    count = 0
    for i in lst:
        count = count + int(i)
    return count
points
Submitted by Chris01178
over 8 years

Answer 55f279fdd3292f0fcb000705

0 votes

Permalink

def digit_sum(n): total = 0

for i in str(n):
    total += int(i)
return total
points
over 7 years