A problem about range() in Digit_Sum



By running my code, the python returned this warning: TypeError: 'int' object has no attribute 'getitem'

What's wrong with my code?
And when I ran the sum() to add two int(), it returned TypeError: 'int' object is not iterable. What does it mean?

def digit_sum(n):
    for i in range(len(str(n))):
        print sum(n[i])        


#Why can't sum() add two int()? and why the warning was different?

print sum(int(1)+int(3))


just like you would do in math, to add two integers together we can use plus (+)

sum() is a built in function to calculate the sum of an iterable (like a list)


The reason was that n was an integer and int[ ] was illegal. Thank you anyway.


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