+ 1

Why does this make an error and how to fix this??

I'm trying to make a wordle game but on python In repl.it Whenever I try to type a new word after the first one it gives me an error. Traceback (most recent call last): File "main.py", line 58, in <module> checkWord() File "main.py", line 38, in checkWord gameBoard[0][wordIndex] = key.upper() IndexError: list assignment index out of range Link to repl.it (fork it) : https://replit.com/@DenisDziganchuk/Word-Game#main.py code : # © Copyright 2022 by Denis Dziganchuk from colors import colored from getkey import getkey, keys from words import words import os import string word = words[0] gameRunning = True wordMade = False wordIndex = 0 gameBoard = [[" ", " ", " ", " ", " "] for i in range(6)] blank = [["|| |", "| |", "| |", "| |", "| ||"] for i in range(6)] allowed = list(string.ascii_lowercase) allowed.append(keys.ENTER) allowed.append(keys.BACKSPACE) def printGameBoard(): global gameBoard print("-----------------------------------------------") for i in range(6): middle = ["|" + el.center(7) + "|" for el in gameBoard[i]] print("".join(blank[i])) print("|" + "".join(middle) + "|") print("".join(blank[i])) print("-----------------------------------------------") def checkWord(): global wordMade, gameBoard, wordIndex, blank typedWord = "" key = getkey() while key.lower() not in allowed: key = getkey() if key in list(string.ascii_letters): gameBoard[0][wordIndex] = key.upper() wordIndex += 1 elif key == keys.ENTER: wordMade = True typedWord = "".join(gameBoard[0]) for i in range(5): if word[i] == typedWord[i]: blank[0][i] = colored(" ", "grey", "on_green") elif key == keys.BACKSPACE: gameBoard[0][wordIndex - 1] = " " wordIndex -= 1 clearBoard = lambda: os.system("clear") while gameRunning: clearBoard() printGameBoard() checkWord()

16th May 2022, 11:22 PM
Ailana
Ailana - avatar
2 Answers
0
It's at it says. You are using an index out of range. Print "wordIndex" before use to see what values it gets before the error.
17th May 2022, 6:32 AM
Mustafa A
Mustafa A - avatar
0
Who is this and why are they using my code from over a year ago? I wouldn't like this to show up when someone searches my name. If there's a way to delete this, please do.
30th Nov 2023, 6:56 PM
Denis D