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

Dung Do Tien Sep 05 2021 223

The function playSound takes a list of integers and will play a sound for every single number. Thus, if one of the numbers in the list is - 1, then it 1has a certain 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 tried several ways to convert the list to integers. I'm not too sure what I need to change. I'm sure there is a more efficient way to do this. Any help would be greatly appreciated.

Have 3 answer(s) found.
  • C

    CopterJS Sep 05 2021

    For me, I was getting this error because I needed to put the arrays in paratheses. In this case, the error is a little more complicated ...

    i.e. concatenate((a, b))  correct way

     concatenate(a, b) incorrect

    hope this helps.

  • m

    majid gholipour Sep 05 2021

    The answer is that you are passing your list as an input argument in range() method that expects only an integer. Do not do this. Say instead for i in myList.

  • R

    Rahul Bagale Sep 05 2021

    The error is this:

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

    You cannot pass a list to range() that expects an integer. Most likely you intended 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):

     

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close