Home > Software engineering >  "IndexError: list assignment index out of range" assigning to an index in an empty list
"IndexError: list assignment index out of range" assigning to an index in an empty list

Time:10-03

#all leter are MAG
#E->3 A->4 O->0 V->W I->2
#rand ASCII with ch
from random import randint
letterNum=randint(4,8)
word=[]
if len(word)==0:
  faseLetter=chr(randint(65,90))
  word[0]=faseLetter
  voc=['A','E','O','U','Y','I']
for i in range (letterNum 1):
  firstLetter=word[letterNum]
  randomChangeMaker=randint(1,2)
  if firstLetter in ['A','E','O','U','Y','I']:
    faseLetter=chr(randint((66,68),(70,72),(74,78),(80,88),(90)))
    if faseLetter == 'W' and randomChangeMaker == 2:
      faseLetter='V'
  else:
    faseLetter=chr(randint(65,90))
    if faseLetter == 'E' and randomChangeMaker == 2:
      faseLetter='3'
    elif faseLetter == 'A' and randomChangeMaker == 2:
      faseLetter='4'
    elif faseLetter == 'O' and randomChangeMaker == 2:
     faseLetter='0'
    elif faseLetter == 'V' and randomChangeMaker == 2:
      faseLetter='W'
    elif faseLetter == 'W' and randomChangeMaker == 2:
      faseLetter='V'
    elif faseLetter == 'I' and randomChangeMaker == 2:
      faseLetter='2'
  word=word faseLetter
print(word)

that's my code and I'm getting this error(I'm using replit.com editor)

Traceback (most recent call last): File "main.py", line 10, in word[0]=faseLetter IndexError: list assignment index out of range

CodePudding user response:

Your problem can be shorted to

>>> word = []
>>> word[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

From Mutable Sequence Types

s[i] = x      item i of s is replaced by x

The key here is "replaced". The item has to exist to be replaced. instead, you can append

word.append(1)

CodePudding user response:

Well, word is empty, so you can't assign anything to its zeroth member since there is no such member. (JavaScript might let you do this, but that's JavaScript for you, eh?)

Either:

  1. word.append(faseLetter) to the empty list instead of word[0] = faseLetter, so it becomes a list of length 1
  2. Initialize word with a "fake" zeroth member, i.e. word = [None] instead of word = [] so you can assign to that zeroth member.

CodePudding user response:

The list has not elements at the point of the error (you're even checking this! len(word) == 0), so the index 0 is invalid for the word list. What you want to do is:

word.append(faseLetter)

This is how we add new elements to a list in Python, the list[index] = value syntax works only if the list's length is at least index 1.

CodePudding user response:

What's the length of the array word? referring to line 10 you said word[0]=faseletter.. but since you didn't initiate the array word size then you can't do that. You have to use append in this case.

CodePudding user response:

Get these clear.

letterNum is being assigned a value between 4 to 8.

word.append(faseLetter) is appending one letter at 0th index.

in for loop firstLetter=word[letterNum] (Considering letterNum is between 4-8) it will always give error as length of list word is just one and word[letterNum] is trying to get element form index which does not exit.

faseLetter=chr(randint((66,68),(70,72),(74,78),(80,88),(90)))

will give error as following

randint() takes 3 positional arguments but 6 were given

Because

random.randint only takes two arguments, a start and an end. The third argument mentioned by Python is self, which is done automatically.

  • Related