0

I'm getting the error list assignment index out of range when trying to assign values to an array. Why am I getting this error and how do I fix it?

array_questions = input("How many words would you like to search for? (maximum of 5): ")
to_search_words = []

x = int(array_questions)
y = 0
z = y + 1

while x > 0:
    y + 1
    str = ("%d. " % z)
    to_search_words[y] = input(str)
davidism
  • 121,510
  • 29
  • 395
  • 339
gacl
  • 31
  • 1
  • 1
  • 4

1 Answers1

0

Becuase you're using list, you may want to use append, which is part of every list's API:

Add an item to the end of the list. Equivalent to a[len(a):] = [x].

Example:

while x > 0:
    # not needed # y + 1
    str = ("%d. " % z)
    to_search_words.append(input(str))
    x -= 1 # prevent infinite loop
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88