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


#1

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

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


#2

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


#3

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!


#4

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)


#5

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


#6

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: ")
digit_sum(numeros)
digit_sum("434")
digit_sum("12345")

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)


#7

Many thanks once again for your precious help!

Now the error is different!

Can you help me?


#8

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


#9

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: ")
digit_sum(numeros)

#10

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


#11

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!


#12

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

Can i see how you did it?


#13

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!


#14

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