4. Digit_sum help


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
4.Digit_sum

<In what way does your code behave incorrectly? Include ALL error messages.>
It only adds the firt number.
Oops, try again. Your function fails on digit_sum(434). It returns 4 when it should return 11.

<What do you expect to happen instead?>
It should have added ll the unmbers

```python

def digit_sum(n):
num_str = str(n)
index = 0
for i in range(len(num_str)):
total = 0
total = total + int(num_str[i])
return total

<do not remove the three backticks above>

#2

Hi @paulcherian,

Though it is not obvious in your post, because your code is not formatted, this statement is probably indented too much, making it part of the for loop …

        return total

That would cause the loop to terminate during its first iteration.


#3

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