Python 类型错误:“列表”对象不能解释为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28036309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:39:15  来源:igfitidea点击:

TypeError: 'list' object cannot be interpreted as an integer

pythonlistfor-looptypeerror

提问by Greysus

The playSoundfunction is taking a list of integers, and is going to play a sound for every different number. So if one of the numbers in the list is 1, 1has a designated sound that it will play.

playSound函数接受一个整数列表,并将为每个不同的数字播放声音。因此,如果列表中的数字之一是1,则1具有将播放的指定声音。

def userNum(iterations):
  myList = []
  for i in range(iterations):
    a = int(input("Enter a number for sound: "))
    myList.append(a)
    return myList
  print(myList)

def playSound(myList):
  for i in range(myList):
    if i == 1:
      winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

I am getting this error:

我收到此错误:

TypeError: 'list' object cannot be interpreted as an integer

I have tried a few ways to convert the list to integers. I am not too sure what I need to change. I am sure that there is a more efficient way of doing this. Any help would be very greatly appreciated.

我尝试了几种方法将列表转换为整数。我不太确定我需要改变什么。我相信有一种更有效的方法可以做到这一点。任何帮助将不胜感激。

回答by óscar López

You should do this instead:

你应该这样做:

for i in myList:
    # etc.

That is, remove the range()part. The range()function is used to generate a sequence of numbers, and it receives as parameters the limits to generate the range, it won't work to pass a listas parameter. For iterating over the list, just write the loop as shown above.

也就是移除range()零件。该range()函数用于生成数字序列,它接收生成范围的限制作为参数,将列表作为参数传递是行不通的。要迭代列表,只需编写如上所示的循环。

回答by óscar López

rangeis expecting an integer argument, from which it will build a range of integers:

range期待一个整数参数,它将从中构建一个整数范围:

>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

Moreover, giving it a list will raise a TypeErrorbecause rangewill not know how to handle it:

此外,给它一个列表会引发一个TypeError因为range不知道如何处理它:

>>> range([1, 2, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object cannot be interpreted as an integer
>>>


If you want to access the items in myList, loop over the list directly:

如果要访问 中的项目myList,请直接遍历列表:

for i in myList:
    ...

Demo:

演示:

>>> myList = [1, 2, 3]
>>> for i in myList:
...     print(i)
...
1
2
3
>>>

回答by MattDMo

In playSound(), instead of

playSound(), 而不是

for i in range(myList):

try

尝试

for i in myList:

This will iterate over the contents of myList, which I believe is what you want. range(myList)doesn't make any sense.

这将遍历 的内容myList,我相信这是您想要的。range(myList)没有任何意义。

回答by Michael Aaron Safyan

The error is from this:

错误来自于此:

def playSound(myList):
  for i in range(myList): # <= myList is a list, not an integer

You cannot pass a list to rangewhich expects an integer. Most likely, you meant to do:

您不能传递一个range需要整数的列表。最有可能的是,您打算这样做:

 def playSound(myList):
  for list_item in myList:

OR

或者

 def playSound(myList):
  for i in range(len(myList)):

OR

或者

 def playSound(myList):
  for i, list_item in enumerate(myList):

回答by Saher Ahwal

remove the range.

删除range.

for i in myList

range takes in an integer. you want for each element in the list.

range 接受一个整数。你想要列表中的每个元素。

回答by Malik Brahimi

def userNum(iterations):
    myList = []
    for i in range(iterations):
        a = int(input("Enter a number for sound: "))
        myList.append(a)
    print(myList) # print before return
    return myList # return outside of loop

def playSound(myList):
    for i in range(len(myList)): # range takes int not list
        if i == 1:
            winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

回答by jez

Error messages usually mean preciselywhat they say. So they must be read very carefully. When you do that, you'll see that this one is not actually complaining, as you seem to have assumed, about what sort of object your list contains, but rather about what sort of object it is. It's not saying it wants your list to contain integers (plural)—instead, it seems to want your list to be an integer(singular) rather than a list of anything. And since you can't convert a list into a single integer (at least, not in a way that is meaningful in this context) you shouldn't be trying.

错误消息通常是指正是他们在说什么。因此,必须非常仔细地阅读它们。当你这样做时,你会发现这个实际上并没有像你假设的那样抱怨你的列表包含什么类型的对象,而是抱怨它什么类型的对象。这并不是说它希望您的列表包含整数(复数)——相反,它似乎希望您的列表是一个整数(单数)而不是任何内容的列表。而且由于您无法将列表转换为单个整数(至少,在这种情况下不是有意义的方式),因此您不应该尝试。

So the question is: why does the interpreter seem to want to interpret your list as an integer? The answer is that you are passing your list as the input argument to range, which expects an integer. Don't do that. Say for i in myListinstead.

所以问题是:为什么解释器似乎要将您的列表解释为整数?答案是您将列表作为输入参数传递给range,它需要一个整数。不要那样做。换个说法for i in myList

回答by Ben Arnao

For me i was getting this error because i needed to put the arrays in paratheses. The error is a bit tricky in this case...

对我来说,我收到此错误是因为我需要将数组放在 paratheses 中。在这种情况下,错误有点棘手......

ie. concatenate((a, b))is right

IE。concatenate((a, b))是对的

not concatenate(a, b)

不是 concatenate(a, b)

hope that helps someone lol

希望能帮助某人哈哈