Digit_sum


#1



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


Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "'int' object is not iterable" error.


i expect this thing to work because if it won't ill break the computer


def digit_sum(n):
    str(n)
    ds=0
    for digit in n:
        int(digit)
        ds+=digit
    return ds    
print digit_sum(152)


#2

There are two problems in your code, take a look at these lines:

str(n)
int(digit)

str and int converts given value to the specified type, but they are not changing the original value. So when you do str(n) value of n is still a number, you have to assign this new value to your object:

def digit_sum(n):
    n = str(n)
    ds=0
    for digit in n:
        digit = int(digit)
        ds+=digit
    return ds    
print digit_sum(152)

#3

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