# 4. digit_sum - code works perfectly in jupyter notebook, but won't pass on codeacademy

#1

This code seems to work perfectly in jupyter notebook:

def digit_sum(n):
sum=0
for x in n:
sum += int(x)
return sum

n=raw_input("Enter a number: ")

print "The sum of the digits is %s." %(str(digit_sum(n)))

But I get this error in codeacademy:
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.

#2

Did you assign anything to the name `sum` outside your function? That would affect the submission test which assumes that's left untouched. (built-in functions such as `sum` should not be overwritten)

You shouldn't be doing that inside the function either, but that won't have any lasting effect so that's not what's causing the error.

That's as far as I can get without code that I can try myself, please take care to ensure that any code you post is intact and can be copied to reproduce what you describe.

#3

No, I didn't assign anything to the name outside my function. I'm new to this, but should've known not to use 'sum' as variable. I changed it to s, anyway.
This code is copied and pasted directly from Jupyter Notebook, it's just that the indendations don't show up. Here is is again:

def digit_sum(n):
s=0
for x in n:
s += int(x)
return s

n=raw_input("Enter a number: ")
print "The sum of the digits is %s.\n" %(str(digit_sum(n)))

and a screen shot which shows the indentation and (correct) output:

Thanks again!

#4

Oh. I managed to misread that error as: "int object is not callable"
But you're trying to iterate through an int.

n is an int, and you loop through n. Can't do that.

Code that can be copied and played around with makes things much easier! A screen shot is slightly better, but still can't be copied. It can be re-typed, but at extra effort and even then it might not become a perfect copy, especially if there's something subtle wrong that would just get corrected by the person re-typing it.

#5

Thanks, Ionatan.

The code that's there is copied and pasted directly from Jupyter Notebook, it's just that the indentations will not show. They seem to show in the preview, but when I submit it, they disappear. I'm trying it again using the <>"Preformatted Text" option. Hopefully it works:

``````def digit_sum(n):
s=0
for x in n:
s += int(x)
return s

n=raw_input("Enter a number: ")
print "The sum of the digits is %s.\n" %(str(digit_sum(n)))``````

Regarding your comment re n, it is not an integer. It is a string.

Hopefully the code comes through in a format you can copy and paste and you can try it.

#6

But it is, according to specifications!
As you said, it "works" in your own test. That's because you're not testing the same thing.

Most forums apply some formatting stuff to posts, so code has to be marked as code to be left alone.
Indent each line by an extra 4 spaces, or put ``` before and after the code on their own lines

I see you got it now in your edit

#7
``````def digit_sum(n):
s=0
for x in n:
s += int(x)
return s

n=raw_input("Enter a number: ")
print "The sum of the digits is %s.\n" %(str(digit_sum(n)))``````

I'm sorry, I don't understand. It really looks to me like n is a string:

• So n=raw_input("Enter a number: ") produces a string

• the function digit_sum changes it into an integer so it can be added to the other integer s

• the function returns the integer s

• the final statements converts it back to a string and prints it

#8

Yes. You give your function a string. But the instructions say that your function should accept an integer, so therefore n is an integer according to specifications (instructions)

``print digit_sum(1245)``

#9

Ok, thanks, Ionatan. The code worked perfectly and so I couldn't see what the problem was, but now that I look back at the instructions, they did indeed want the function to take an integer, not a string. Thanks for pointing that out.

An easy fix which passes the digit_sum lesson:

``````def digit_sum(n):
s=0
y=str(n)
for x in y:
s += int(x)
return s

n=int(raw_input("Enter a number: "))
print "The sum of the digits is %s.\n" %(str(digit_sum(n)))``````

#10

Some programming contests like to add a bunch of useless information to their problem descriptions, just because reading instructions is hard, and so they make that part of the contest!

#11

It's my weakness.
Thanks for your patience and help!