digit_sum Why is this wrong?


#1

def digit_sum(n):
total = 0
n = str(n)
for i in n:
total += int(i)
return total

digit_sum(434) <--- returns 4, should return 11


#2

def digit_sum(n):
total = 0
n = str(n)
for i in n:
total += int(i)
return total

digit_sum(434)indent preformatted text by 4 spaces


#3

sorry... I tried to show the indents, but failed


#4

if you edit/update your question, leave a reply so i get a notification. Take the following steps to show indent, i think the backtick is the easiest method of the two:

select your code and press ctrl + k (or cmd + k if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard


#5

i don't see anything when returned, so i added a print: (for the rest the code is the same)

def digit_sum(n):
    total = 0
    n = str(n)
    for i in n:
        total += int(i)
    return total

print(digit_sum(434))

it returns 11, and this your code. Are you sure your return is indented correctly? if return is at the same line as total, it will return 4 since the loop is only executed once


#6

Thank you, stetim94. The problem, was, indeed, the return indent.