4. digit_sum Works in Peppermint but not here


#1



Stuck on 4. digit_sum


I keep getting an error saying the code returns the wrong number.


I have tested my code in peppermint and it works perfect, but copy/paste to codecademy doesn't work.


total = 0

my_list=[]
my_list2=[]

def digit_sum(n):
    n=abs(n)
    y = str(n)
    total = 0
    for each in y:
        my_list.append(each)
    for each in my_list:
        my_list2.append(int(each))
    for each in my_list2:
        total = total + each
    return total


#2

I have tried your code myself, and even did a

print total

at the very end of the function and it came out correctly. There shouldn't be anything wrong with it. I think it may have something to do with how codeacademy console reads the results for this is particular exercise. Perhaps some of the mods can help you out.

Btw, you can make your code much neater if you do this:

def digit_sum(n):
    y = abs(n)
    total = 0
    for each in (str (y)):
        my_list.append(each)
    for each in my_list:
        total += int(each)
    print "Sum of the number is", total
    return total

Maybe you can try without a list first?


#3

Got the same problem with this code. work just fine in python shell but not in CA.

numbers = []

    
def digit_sum(n):
    result = 0
    strn = str(n)
    for char in strn:
        numbers.append(char)
    for e in numbers:
        result += int(e)
    print result
    return result

#4

Thanks so much for taking a look, and thanks for the advice. I'll definitely give that a try too. ! :slight_smile:


#5

Works no less fine on codecademy, as you'll find if you do your same tests on the website as you're doing locally, you'll get the same result. However, codecademy runs its own tests, that's what your functions are failing.

Here's a better test, which you can add at the bottom of your code:

for n in range(10):
    result = digit_sum(n)
    correct = sum(map(int, str(n)))
    if result != correct:
        print 'digit_sum({}) incorrectly returned {} instead of {}'.format(n, result, correct)

I think the lesson to be learned here is that one correct outcome doesn't prove something correct. Unless you test all possibilities, then you have not proven it correct by just testing.

And also, if you change environment and do different tests on each one, then blaming the environment is not a correct conclusion! You would need to perform the same tests in order to do that, and get it to produce different results in both environments.


4. digit_sum: adding items in the list
#6

thanks!!! trying it out now :relaxed:


#8

Hey, I was suffered because of 4/15 too. Also, I kinda cheated on some of the answers above and found it, it works in CA.

numbers = []
def digit_sum(n):
    result = 0
    strn = str(n)
    for char in strn:
        numbers.append(char)
        for i in char:
            i = int(char)
            result += i
    print result
    return result

#9

wow i have the same problem but it fix now thank you


#11

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