In: Computer Science
I am building a tik tac toe board using python and the function I am using is not working to check to see if the board is full. I'll include the code for the board and the function to check if it is full. X's and O's are being inserted and the function is suppose to stop running when is_full(board) returns True. ch is either x or o
board = []
for i in range(3):
board.append([])
for j in range(3):
board[i].append(' ')
return board
def is_full(board):
""" Checking for board full """
for lst in board:
for ch in lst:
if ch == ' ':
return False
def is_full(board):
# checks if the list contains either 'X' or 'O'
# if character is not among both it returns False
# if all the characters are among them it returns True
for lst in board:
for ch in lst:
if ch not in ['X','O']:
return False
return True
board = [
['X', 'O', 'X'],
['O', 'X', 'O'],
['O', 'O', 'X'],
]
print(is_full(board))
board = [
['X', ' ', 'X'],
['O', 'X', 'O'],
['O', 'O', 'X'],
]
print(is_full(board))
Code :
Output :