views:

303

answers:

6
inp = int(input("Enter a number:"))

for i in inp:
    n = n + i;
    print (n)

... throws an error: 'int' object is not iterable

I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?

Thanks

+3  A: 

try:

for i in str(inp):

That will iterate over the characters in the string representation. Once you have each character you can use it like a separate number.

ghills
Hmm..it says TypeError: unsupported operand type(s) for +: 'int' and 'str'
Nimbuz
convert back to an int when adding?
Brian R. Bondy
Well when you go to add it to an integer, it is now a string. So in that case you could say int(i) + n or whatever it is.
ghills
A: 

for .. in statements expect you to use a type that has an iterator defined. A simple int type does not have an iterator.

Brian R. Bondy
+1  A: 

Well, you want to process the string representing the number, iterating over the digits, not the number itself (which is an abstract entity that could be written differently, like "CX" in Roman numerals or "0x6e" hexadecimal (both for 110) or whatever).

Therefore:

inp = input('Enter a number:')

n = 0
for digit in inp:
     n = n + int(digit)
     print(n)

Note that the n = 0 is required (someplace before entry into the loop). You can't take the value of a variable which doesn't exist (and the right hand side of n = n + int(digit) takes the value of n). And if n does exist at that point, it might hold something completely unrelated to your present needs, leading to unexpected behaviour; you need to guard against that.

This solution makes no attempt to ensure that the input provided by the user is actually a number. I'll leave this problem for you to think about (hint: all that you need is there in the Python tutorial).

Michał Marczyk
+8  A: 

First, lose that absurd call to int -- which is taking you farther from what you want, so, what ever possessed you to put it in?! Change:

inp = int(input("Enter a number:"))

to the simpler

inp = input("Enter a number:")

so that inp is a string of digits and you can indeed loop over it, digit by digit.

Next, assign some initial value to n -- as you code stands right now, you'll get a NameError since you never initialize it. Presumably you want n = 0 before the for loop.

Next, consider the difference between a character and an integer again. You now have:

n = n + i;

which, besides the utterly absurd (but innocuous) semicolon, is trying to sum the character i to the integer n -- that won't work! So, this becomes

n = n + int(i)

to turn character '7' into integer 7, and so forth.

Alex Martelli
+1  A: 

Side note: if you want to get the sum of all digits, you can simply do

print sum(int(digit) for digit in raw_input('Enter a number:'))
EOL
A: 

As ghills had already mentioned

inp = int(input("Enter a number:"))

n = 0
for i in str(inp):
    n = n + int(i);
    print n

When you are looping through something, keyword is "IN", just always think of it as a list of something. You cannot loop through a plain integer. Therefore, it is not iterable.

JohnWong