4/15 digit_sum pls help me


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096


Oops, try again. Your function fails on digit_sum(434). It returns 4 when it should return 11.


i thin it should be fine but it is not


Replace this line with your code. 
def digit_sum(n):
    string=str(n)
    for letter in string:
        total=0
        number=int(letter)
        total+=number
    return total


#3

The problem is that the

 total=0

is part of the loop. So for example for 434 the loop goes:

first loop:
(letter is "4")
(total is not defined)
total = 0
(total is 0)
number = int("4")
total+=4
(total is 4)

second loop:
(letter is "3")
(total is 4)
total = 0
(total is 0)
number = int("3")
total+=3
(total is 3)

third loop:
(letter is "4")
(total is 3)
total = 0
(total is 0)
number = int("4")
total+=4
(total is 4)

#4

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