15.4. I don't know what I did wrong?! Can anybody help me out?


This is the excercise I am stuck at:

I have no idea what I did wrong, this is the error message I get when I run my code:
Traceback (most recent call last):
File "python", line 11, in
File "python", line 4, in digit_sum
TypeError: 'int' object is not iterable

I also get this error at the bottom of my screen:
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.

Replace this line with your code. 
This is my code: digits_sum = 0

def digit_sum(x):
    for number in x:
         strings = str(x)
    for item in strings:
        seperate_numbers = int(item)
    for digit in seperate_numbers:
        digits_sum += digit

print digit_sum(1234)



for number in x:

x is a integer, you can't loop over integers (integers are not iterable)

make sure that everything you need to calculate the digit_sum is inside the function (including declaring digits_sum)


For the code above, I think the two steps are just flipping around with integers and strings. For example, strings=str(123)="123", and then you went back to the original step and turned seperate_numbers=int("123")=123, rather than 1,2,3

Good luck!


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