why does the compiler give me list index out of bounds error?

48 Views Asked by At

x = int(input("Row :"))
y = int(input("Column:"))  
z = y+x\*8-9

board = \[\]

def ShowBoard():
j = 0

    for i in range(64):
        if i == z:
            board.append('0')
        else:
            board.append('_')
    
    for item in board:
        j+=1
        if j%8 == 0:
            print(item,"\n")
        else:
            print(item, end = "   ")
            
    return board

Winner = False

def ProveGame(z, board):

    for i in range(64): 
        if z%8 > 3 and board[z] == board[z+9] == board[z+18] == board[z+27] == '0':
            Winner == True
            print("4 in a right-diagonal. You won!")
        if z%8<3 and board[z] == board[z+7] == board[z+14] == board[z+21] == '0' :
            Winner == True
            print("4 in a left-diagonal. You won!")
        if z%8>3 and board[z]==board[z+1]==board[z+2]==board[z+3]=='0':
            Winner == True
            print("4 in a row. You won!")
        if z<41 and  board[z] == board[z+8] == board[z+16] == board[z+24]=='0':
            Winner == True
            print("4 in a column. You won!")

while(Winner == False):  
ProveGame(z,board)
ShowBoard()

Ive been trying to code the game Connect4. My input was Row=5 and Column=5 and the compiler gives me this:

PS C:\\Users\\User\> python -u "C:\\Users\\User\\AppData\\Local\\Temp\\tempCodeRunnerFile.python"
Zeile :5
Spalte:5
Traceback (most recent call last):
File "C:\\Users\\User\\AppData\\Local\\Temp\\tempCodeRunnerFile.python", line 45, in \<module\>
ProveGame(z,board)
File "C:\\Users\\User\\AppData\\Local\\Temp\\tempCodeRunnerFile.python", line 31, in ProveGame
if z%8 \> 3 and board\[z\] == board\[z+9\] == board\[z+18\] == board\[z+27\] == '0':
\~\~\~\~\~^^^
IndexError: list index out of range

Why?

0

There are 0 best solutions below