Python - TypeError: 'int'对象不可迭代

时间:2021-05-12 20:17:58

Here's my code:

这是我的代码:

import math

print "Hey, lets solve Task 4 :)"

number1 = input ("How many digits do you want to look at? ")
number2 = input ("What would you like the digits to add up to? ")

if number1 == 1:
    cow = range(0,10)
elif number1 == 2:
    cow = range(10,100)
elif number1 == 3:
    cow = range(100,1000)
elif number1 == 4:
    cow = range(1000,10000)
elif number1 == 5:
    cow = range(10000,100000)
elif number1 == 6:
    cow = range(100000,1000000)
elif number1 == 7:
    cow = range(1000000,10000000)
elif number1 == 8:
    cow = range(10000000,100000000)
elif number1 == 9:
    cow = range(100000000,1000000000)
elif number1 == 10:
    cow = range(1000000000,10000000000)

number3 = cow[-1] + 1

n = 0
while n < number3:
    number4 = list(cow[n])
    n += 1

I am looking to make a loop so that for each element in the list, it will get broken down into each of it's characters. For example, say the number 137 was in the list then it would be turned into [1,3,7]. Then I want to add these numbers together (I haven't started that bit yet but I have some idea of how to do it).

我正在寻找一个循环,以便对于列表中的每个元素,它将被分解成每个字符。例如,如果数字137在列表中,那么它就会变成[1,3,7]。然后我想把这些数字加在一起(我还没有开始,但是我知道怎么做)。

However, I keep getting the error message

但是,我一直得到错误消息。

TypeError: 'int' object is not iterable

when I try and run this.

当我试着运行这个。

What am I doing wrong?

我做错了什么?

1 个解决方案

#1


58  

Your problem is with this line:

你的问题是这样的:

number4 = list(cow[n])

It tries to take cow[n], which returns an integer, and make it a list. This doesn't work, as demonstrated below:

它尝试获取cow[n],它返回一个整数,并使它成为一个列表。这是行不通的,如下所示:

>>> a = 1
>>> list(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

Perhaps you meant to put cow[n] inside a list:

也许你是想把牛放在一个名单里:

number4 = [cow[n]]

See a demonstration below:

见下面的演示:

>>> a = 1
>>> [a]
[1]
>>>

Also, I wanted to address two things:

另外,我想谈两点:

  1. Your while-statement is missing a : at the end.
  2. 你的陈述漏掉了a:最后。
  3. It is considered very dangerous to use input like that, since it evaluates its input as real Python code. It would be better here to use raw_input and then convert the input to an integer with int.
  4. 使用这样的输入被认为是非常危险的,因为它将其输入作为真正的Python代码进行评估。这里最好使用raw_input,然后将输入转换为整数。

To split up the digits and then add them like you want, I would first make the number a string. Then, since strings are iterable, you can use sum:

要把数字分开,然后像你想的那样加起来,我首先要把数字变成字符串。那么,由于字符串是可迭代的,所以可以使用sum:

>>> a = 137
>>> a = str(a)
>>> # This way is more common and preferred
>>> sum(int(x) for x in a)
11
>>> # But this also works
>>> sum(map(int, a))
11
>>>

#1


58  

Your problem is with this line:

你的问题是这样的:

number4 = list(cow[n])

It tries to take cow[n], which returns an integer, and make it a list. This doesn't work, as demonstrated below:

它尝试获取cow[n],它返回一个整数,并使它成为一个列表。这是行不通的,如下所示:

>>> a = 1
>>> list(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

Perhaps you meant to put cow[n] inside a list:

也许你是想把牛放在一个名单里:

number4 = [cow[n]]

See a demonstration below:

见下面的演示:

>>> a = 1
>>> [a]
[1]
>>>

Also, I wanted to address two things:

另外,我想谈两点:

  1. Your while-statement is missing a : at the end.
  2. 你的陈述漏掉了a:最后。
  3. It is considered very dangerous to use input like that, since it evaluates its input as real Python code. It would be better here to use raw_input and then convert the input to an integer with int.
  4. 使用这样的输入被认为是非常危险的,因为它将其输入作为真正的Python代码进行评估。这里最好使用raw_input,然后将输入转换为整数。

To split up the digits and then add them like you want, I would first make the number a string. Then, since strings are iterable, you can use sum:

要把数字分开,然后像你想的那样加起来,我首先要把数字变成字符串。那么,由于字符串是可迭代的,所以可以使用sum:

>>> a = 137
>>> a = str(a)
>>> # This way is more common and preferred
>>> sum(int(x) for x in a)
11
>>> # But this also works
>>> sum(map(int, a))
11
>>>