Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Quick question! ~~How can i resolve the issue on this python code to enable it p

ID: 3722177 • Letter: Q

Question

Quick question! ~~How can i resolve the issue on this python code to enable it play the game. The screen shot above pointed were the error emanate from. Specifically from the last "else" statement in the code. The full code is shown below.

Your help will be appreciated.

Full code:

# This is a function that displays the boards

def displayBoard(board):

for row in board:

for col in row:

    print(col, end= ' ')

print(" ")

def makeMove(moveVal, board): # This function asks for user to give input for 'X' player and for 'O' player then updates the value in 2D array

   print(moveVal + " what row?", end= ' ')

row = int(input())

print(moveVal + " what col?", end= ' ')

col = int(input())

#ensure the move is legal or ask them again

if (board[row][col] != '_'):

   print ("Illegal move. Enter again ! ")

makeMove(moveVal, board)

board[row][col] = moveVal

def chkBoard(mark, board):

   if (mark == board[0][0] == board[1][1] == board[2][2]):

     return True

   if (mark == board[2][0] == board[1][1] == board[0][2]):

     return True

for i in range(2):

   if (mark == board[i][0] == board[i][1] == board[i][2]):

     return True

   if (mark == board[0][i] == board[1][i] == board[2][i]):

     return True

return False

# this function checks if there is a winner of the game yet

def isWinner(board):

#return true if their is a winner and false otherwise

if (chkBoard('X', board) == True):

   print ("X is winner ")

return True

if (chkBoard('O', board) == True):

   print ("O is winner ")

return True

return False

def clearBoard(board):

#this function should perform the logic to clear the board

   board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]

for rowNum in range(0, len(board)):

for colNum in range(0, len(board[rowNum])):

   board[rowNum][colNum] = '_'

#This is the functions which keeps the game running untill there is a winner

def playGame(board):

moves = ['X', 'O']

currPlayer = 0

while(isWinner(board) == False):

   displayBoard(board)

makeMove(moves[currPlayer], board)

if(currPlayer == 0):

currPlayer = 1

else:

   currPlayer = 0

#we want to ask if they want to play again

user_ip = int(input("Want to play again ?? If yes=1, no=0:"))

if (user_ip == 1):

board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]

playGame(board)

else:
  
print("Exiting..")

board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']] #if yes, clear the board and start over

playGame(board)

print ("sum of x+y =", z);

Explanation / Answer

## Please make sure about Indentation. Do not change the left spaces as it makes statement in or out of the scope for ##function.

def displayBoard(board):

for row in board:

for col in row:

print(col, end= ' ')

print(" ")

def makeMove(moveVal, board): # This function asks for user to give input for 'X' player and for 'O' player then updates the value in 2D array

print(moveVal + " what row?", end= ' ')

row = int(input())

print(moveVal + " what col?", end= ' ')

col = int(input())

#ensure the move is legal or ask them again

if (board[row][col] != '_'):

print ("Illegal move. Enter again ! ")

makeMove(moveVal, board)

board[row][col] = moveVal

def chkBoard(mark, board):

if (mark == board[0][0] == board[1][1] == board[2][2]):

return True

if (mark == board[2][0] == board[1][1] == board[0][2]):

return True

for i in range(2):

if (mark == board[i][0] == board[i][1] == board[i][2]):

return True

if (mark == board[0][i] == board[1][i] == board[2][i]):

return True

return False

# this function checks if there is a winner of the game yet

def isWinner(board):

#return true if their is a winner and false otherwise

if (chkBoard('X', board) == True):

print ("X is winner ")

return True

if (chkBoard('O', board) == True):

print ("O is winner ")
return True

return False

def clearBoard(board):

#this function should perform the logic to clear the board

board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]

for rowNum in range(0, len(board)):

for colNum in range(0, len(board[rowNum])):

board[rowNum][colNum] = '_'

#This is the functions which keeps the game running untill there is a winner

def playGame(board):

moves = ['X', 'O']

currPlayer = 0

while(isWinner(board) == False):

displayBoard(board)

makeMove(moves[currPlayer], board)

if(currPlayer == 0):

currPlayer = 1

else:

currPlayer = 0

#we want to ask if they want to play again

user_ip = int(input("Want to play again ?? If yes=1, no=0:"))

if (user_ip == 1):

board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]

playGame(board)

else:
  
print("Exiting..")

board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']] #if yes, clear the board and start over

playGame(board)