Digit_sum: Need help


#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.


Have hit a bit of a wall here. Need help, or at least a pointer in the right direction because I think I have got the gist of things, it's just that I feel that I'm missing something.


def digit_sum(n):
    total = 0
    x = str(n)
    y = len(x)
    for a in x:
        total = total + 1
    print x 
    print y
    print total
    
    
    # str()
    # int()

digit_sum(1234)


#2

for starters, you should return the sum (which is stored in total), you currently only print total

i think you need to take another look at your math, this is good:

for a in x:

now a will contain each digit, which you need to add to total

but remember, a contains a digit as string, to add it to total correctly, you need to convert it back to integer first


#3

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