4/15 digit_sum


#1

Can someone explain me:
1- What am I doing wrong here as the result is as expected
2- What should I do instead (without giving the solution... that is, explain me in plain text)

Here is the code I wrote:

num = raw_input('number:')
str(num)
def num_into_list(num_integer):
    liste=[]
    for i in num_integer:
        liste.append(i)
    return liste
def digit_sum(lst):
    sul=0
    for i in lst:
        sul+=int(i)
    return sul
z = num_into_list(num)
digit_sum(z)
print digit_sum(z)

#2

If the result is as expected, then what is the problem?
(I'm sure I can find out by running your code in the exercise, but question-asking is essentially how you start debugging on your own anyway so might as well start with that.)


#3

The problem is that codecademy doesn't accept my code; the error message is:"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."
Sorry I forgot to mension that!


#4

So the submission test called your function and got that error.
But when you call your function, you don't.

That suggests that you are calling it differently from the submission test, right? How should it be called?


#5

OOOwwww.... thanks !
So it's just that I think differently from the 'submission test'... I am going to try another way. Calling a function that has an argument of a different kind right (i.e digit_sum should have an argument that is not a list) ?


#6

Right, your function should expect one argument, an integer (an instance of the int type)


#7

Thanks a lot, I was stuck there and I didn't know why !


#8

hahaha from 15 lines down to... 6 !!!!
Thanks codecademy an thanks a lot ionatan :smile:


#9

I ended up with three lines via map.

 def digit_sum(n):
     o = sum(list(map(int, str(n))))
     return o

Edit: figured out the indentation in the forum…