Digit_sum


#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 None when it should return 11.


i expected no error.


Replace this line with your code. 
def digit_sum(n):
    total=0
    n=str(n)
    for x in n:
        total= total + int(x)
    print total
print digit_sum(434)


#2

your function prints the total, the exercise wants you to return the total


#3

def digit_sum(n):
total=0
n=str(n)
for x in n:
total= total + int(x)
return total
print digit_sum(434)

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


#4

def digit_sum(n):
    total=0
    n=str(n)
    for x in n:
        total= total + int(x)
        return total
print digit_sum(434)

#5

A post was split to a new topic: Digit_sum


#6

A function ends the moment a return keyword is reached, if a return keyword is reached in a loop, the loop breaks

I said you had to return instead of print, there was nothing wrong with print being after the (instead of inside) the for loop


#8

i understood. thank you!


#9

You're welcome :slight_smile: Good to hear you got it to work and understand :slight_smile:


#10