可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
The playSound
function 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
, 1
has a designated sound that it will play.
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.
回答1:
Error messages usually mean precisely what 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 myList
instead.
回答2:
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...
ie. concatenate((a, b))
is right
not concatenate(a, b)
hope that helps someone lol
回答3:
range
is expecting an integer argument, from which it will build a range of integers:
>>> 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 TypeError
because range
will not know how to handle it:
>>> 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:
for i in myList:
...
Demo:
>>> myList = [1, 2, 3]
>>> for i in myList:
... print(i)
...
1
2
3
>>>
回答4:
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 range
which expects an integer. Most likely, you meant to do:
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):
回答5:
remove the range
.
for i in myList
range takes in an integer. you want for each element in the list.
回答6:
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 list as parameter. For iterating over the list, just write the loop as shown above.
回答7:
In playSound()
, instead of
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.
回答8:
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)