4.Digit_sum


#1

4.Digit_sum

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

It should keep running the code with the other numbers

```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

If anyone can help it would be much appreciated!


#3

Be sure to initialize before the loop.


#4

Thanks for the advice!


#5

Also, this unused variable definition can be safely removed.


#6

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