Digit_sum help!


#1
num = []

def digit_sum(digit):
    digit_str = str(digit)
    for numbers in digit_str :
        num.append(int(numbers)) 
    print sum(num)
digit_sum(434)

it works but there is error message in pages…

help me please~


#2

what happens when you include a second function call:

num = []

def digit_sum(digit):
    digit_str = str(digit)
    for numbers in digit_str :
        num.append(int(numbers)) 
    print sum(num)
digit_sum(434)
digit_sum(525)

you see that the second function call does not give the correct sum, this is because num is accumulating from multiply function calls, given num isn’t defined in the function

also, read the instructions careful, you need to return the digit sum, currently your function only prints the digit sum


#3

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