Reputation: 51
Can anyone tell me where the errors are in this? I have been going around in circles for over a week trying to figure it out. I am getting the error "TypeError: list indices must be integers or slices, not list" for the line of code in the playerXturn() function that is trying to put an "X" where the player chooses the indices.
def main():
gameBoard = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
printBoard(gameBoard)
playerXturn(gameBoard)
while boardFull(gameBoard) is False:
if diagonalWin(gameBoard) is False and rowWin(gameBoard) is False:
playerOturn(gameBoard)
if diagonalWin(gameBoard) is False and rowWin(gameBoard) is False:
playerXturn(gameBoard)
else:
print("Player X wins!")
else:
print("Player O wins!")
printBoard(gameBoard)
def printBoard(gameBoard):
print("---------")
print(gameBoard[0][0], "|", gameBoard[0][1], "|", gameBoard[0][2])
print("---------")
print(gameBoard[1][0], "|", gameBoard[1][1], "|", gameBoard[1][2])
print("---------")
print(gameBoard[2][0], "|", gameBoard[2][1], "|", gameBoard[2][2])
print("---------")
def playerXturn(gameBoard):
playerXrowChoice = input("Enter a row (0, 1, or 2) for player X: ")
playerXrow = [eval(x) for x in playerXrowChoice]
playerXcolumnChoice = input("Enter a column (0, 1, or 2) for player X: ")
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
if gameBoard[playerXrow][playerXcolumn] != "X" and gameBoard[playerXrow][playerXcolumn] != "O":
gameBoard[playerXrow, playerXcolumn] = "X"
else:
print("This spot is already taken.")
return gameBoard
def playerOturn(gameBoard):
playerOrowChoice = input("Enter a row (0, 1, or 2) for player X: ")
playerOrow = [eval(x) for x in playerOrowChoice]
playerOcolumnChoice = input("Enter a column (0, 1, or 2) for player X: ")
playerOcolumn = [eval(x) for x in playerOcolumnChoice]
if gameBoard[playerXrow][playerXcolumn] != "X" and gameBoard[playerXrow][playerXcolumn] != "O":
gameBoard[playerOrow, playerOcolumn] = "O"
else:
print("This spot is already taken.")
return gameBoard
#check for empty spaces on the board
def boardFull(gameBoard):
for i in range(len(gameBoard)):
for j in range(len(gameBoard[i])):
if j != " ":
return True
else:
return False
#check for diagonal match
def diagonalWin(b):
while b[1][1] != " ":
if b[1][1] == b[0][0] == b[2][2] or b[1][1] == b[0][2] == b[2][0]:
return True
else:
return False
#check for 3 in a row
def rowWin(b):
for row in range(0, 3):
if b[row][0] == b[row][1] == b[row][2] == 'X':
return True
else:
return False
main()
Upvotes: 0
Views: 59
Reputation: 161
I'm quite new to python so I don't know what eval() does, but I figured out where is the error.
def playerXturn(gameBoard):
playerXrowChoice= input("Enter a row (0, 1, or 2) for player X: ")
playerXrow = [eval(x) for x in playerXrowChoice]
playerXcolumnChoice= input("Enter a column (0, 1, or 2) for player X: ")
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
if gameBoard[playerXrow][playerXcolumn] != "X" and gameBoard[playerXrow][playerXcolumn] != "O":
gameBoard[playerXrow, playerXcolumn] = "X"
else:
print("This spot is already taken.")
return gameBoard
Here on line:
gameBoard[playerOrow, playerOcolumn] = "X"
here you need to target a specific cell of like this: gameBoard[row][column] in this case:
gameBoard[playerOrow][playerOcolumn] = "X"
and I tried to run the code after this change and I got another error, but I solved it.
As I said I don't know what lines:
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
playerXcolumn = [eval(x) for x in playerXcolumnChoice]
do so I removed them and where you ask for user input:
playerXrowChoice= input("Enter a row (0, 1, or 2) for player X: ")
playerXcolumnChoice= input("Enter a column (0, 1, or 2) for player X: ")
You need to convert input from string to int, so you can target the correct cell of the gameBoard.
Upvotes: 0
Reputation: 18249
The error is on this line:
gameBoard[playerXrow, playerXcolumn] = "X"
I think you meant:
gameBoard[playerXrow][playerXcolumn] = "X"
Upvotes: 1