. Advertisement .
..3..
. Advertisement .
..4..
As advised, I used some code samples in another forum, but it could not improve the problem.
My question is the “typeerror: ‘list’ object cannot be interpreted as an integer” in python-how to solve it?
The command line is:
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)
and the result:
TypeError: 'list' object cannot be interpreted as an integer
What does the message mean? Can you advise me to fix it? If you have other better answers, leave them in the answer box below.
The cause:
Above is the reason of this error because you can not pass a list to a function that expects an integer argument like
range
Solution: This error will be solved when you do:
Or
Or
Exactly what error messages mean. They should be carefully read. You’ll find that it isn’t complaining about the object in your list but about the object in that. It isn’t saying that your list should contain integers (plural), but it does seem to prefer that your list be an int (singular), rather than a collection of any other objects. You shouldn’t try to convert a list into one integer, at least not in a meaningful way in this context.
The question is, why does the interpreter want to interpret your list to be an integer? Your list is being passed to
range
as an input argument.range
expects an integer. Do not do this. Sayfor i in myList
instead.