Search code examples
pythonlistinttypeerror

Python 3.8 "TypeError: 'int' object is not iterable" when trying to add an int to a list


I am making a program where it generates a random number from 1 to 100 and then, if that number isn't already in the list, it adds it to the list and keeps doing that until there is 100 unique numbers in the list. This is my code so far:

from random import randint
numbers = []
loop = True
while loop == True:
  number = randint(1, 100)
  if number in numbers:
      print("{} is there already!".format(number))
  else:
    numbers += number

But it keeps giving me this error:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    numbers += number
TypeError: 'int' object is not iterable

But, as I'm pretty sure, there's nothing wrong with my code. What do I do?


Solution

  • Just replace numbers += number with numbers.append(number).