Digit_sum - Your function fails on digit_sum(434). It returns [] when it should return 11



Hello everyone, I need your help! I can not understand why the error. Can you help me, please?


to validate your function works correctly, the exercise will call the function multiply times (not the whole script). So make sure that everything you use to get the digit_sum, is inside your function


Thanks for your quick reply!

Your suggestion is to put all the code inside the function?

The error continues ...
I apologize if I did not understand your tip!


with the exception of raw_input, given the exercise calls the function multiply times, you don't want to overwrite the function parameter.

place raw_input between line 9 and 10 (outside the function, so no indent) and pass the variable you store the input in as argument when calling the function (you do this already)


Thanks again for your help @stetim94!
Is this what you proposed?


yep, but i hadn't seen yet you made another mistake.

Line 2, you use numeros, which means your function only works for what the user enters when prompted, but as we discussed, the exercise is going to call the function multiply times

So, instead use the function parameter, so your function works for any number supplied as argument when calling the function:

# your function is here, too lazy to type it all

numeros = raw_input("Enter a number: ")

see how i add (which the exercise does behind the scenes) multiply function calls? Your function should work for all this cases. (by using the function parameter, arguments supplied at function call are copied into the function parameter)


Many thanks once again for your precious help!

Now the error is different!

Can you help me?


can you copy paste your code to the forum? This an error i can't debug from just an image


Of course yes!

I choose print here because it is a way to see the code output and error message! :slight_smile:

def digit_sum(numeros):
    results = list(map(int, numeros))
    print results
    b = sum(results)
    print b
    return b

numeros = raw_input("Enter a number: ")


from the instructions:

For example: digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4

1234 is a integer, raw_input gives a string.

to solve this problem, in the first line of your function cast numeros to a string

integers aren't iterable, that is why you get the error message


Thanks for your patience! :slight_smile:

Can you be a little clearer? I have tried to change the data type from int to str but the error continues. I'm getting crazy!


impossible? If you do it right, i get a pass with your code

Can i see how you did it?


TKS! :smiley:

thank you very much for your help!
Giving only a few tips instead of solving the problem is the best way to learn more and better!


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