Digit_sum


#1




Traceback (most recent call last):
File "python", line 8, in
File "python", line 4, in digit_sum
TypeError: 'int' object is not iterable


The error message says that an integer is not iterable. I get that but in the second line of code the integer is turned into a string, so why still this error?


def digit_sum(n):
    str(n)
    sum = 0
    for i in n:
        sum += int(i)
    return sum
    
print digit_sum(1234)


#2

there is a built in function type() which tells you the data type, look at this code:

x = 1234
str(x)
print(type(x))

this will print that type of x is an integer. How is that possible? we do string conversion? Yes, we do, but we don't store the result of this conversion anywhere.


#3

Ok i understand the problem now, thanks a lot!


#4

if you understand the problem, i am sure you can figure out a solution :slight_smile:


#5

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