开发者

int object is not iterable while trying to sum the digits of a number?

开发者 https://www.devze.com 2022-12-14 17:15 出处:网络
I have this code: inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) but it throws an error: \'int\' 开发者_C百科object is not iterable

I have this code:

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

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

but it throws an error: 'int' 开发者_C百科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?


First, lose that call to int - you're converting a string of characters to an integer, which isn't what you want (you want to treat each character as its own number). Change:

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

to:

inp = input("Enter a number:")

Now that inp is a string of digits, you can 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 unnecessary semicolon (Python is an indentation-based syntax), 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.


maybe you're trying to

for i in range(inp)

This will print your input value (inp) times, to print it only once, follow: for i in range(inp - inp + 1 ) print(i)

I just had this error because I wasn't using range()


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.


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).


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:'))


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


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.


Take your input and make sure it's a string so that it's iterable.

Then perform a list comprehension and change each value back to a number.

Now, you can do the sum of all the numbers if you want:

inp = [int(i) for i in str(input("Enter a number:"))]
print sum(inp)

Or, if you really want to see the output while it's executing:

def printadd(x,y):
    print x+y
    return x+y

inp = [int(i) for i in str(input("Enter a number:"))]
reduce(printadd,inp)


One possible answer to OP-s question ("I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?") is to use built-in function divmod()

num = int(input('Enter a number: ')
nums_sum = 0

while num:
    num, reminder = divmod(num, 10)
    nums_sum += reminder


You can try to change for i in inp: into for i in range(1,inp): Iteration doesn't work with a single int. Instead, you need provide a range for it to run.


Don't make it a int(), but make it a range() will solve this problem.

inp = range(input("Enter a number: "))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号