Can you please help me with this Error: “free_field” is not defined!!
Here is the code:
from multiprocessing.dummy import freeze_support
from random import randrange
board = [['1','2','3'],['4','X','6'],['7','8','9']]
def display_board(board):
print("+-------+-------+-------+")
print("| | | |")
print("| "+ board[0][0] +" | "+ board[0][1] +" | "+ board[0][2] +" |")
print("| | | |")
print("+-------+-------+-------+")
print("| | | |")
print("| "+ board[1][0] +" | "+ board[1][1] +" | "+ board[1][2] +" |")
print("| | | |")
print("+-------+-------+-------+")
print("| | | |")
print("| "+ board[2][0] +" | "+ board[2][1] +" | "+ board[2][2] +" |")
print("| | | |")
print("+-------+-------+-------+")
def enter_move(board):
while True:
move = int(input("Choose number from 1:9 "))
if move < 1 or move > 9:
print("Please choose number from 1:9")
continue
elif str(move) not in board[0] and str(move) not in board[1] and str(move) not in board[2]:
print("Only numbers please 1:9")
continue
for row in range (0,3):
for col in range (0,3):
if board[row][col] == str(move):
board[row][col] = 'O'
break
def make_list_of_free_fields(board): #The global is here!!!
global free_field
free_field = []
for row in range (0,3):
for col in range (0,3):
if board[row][col] == 'X' or board[row][col] == 'O':
pass
else:
free_field.append(([row],[col]))
return free_field
def victory_for(board, sign):
if board[0][0] == board[0][1] == board[0][2] == sign:
print(sign, "has Won.")
if board[1][0] == board[1][1] == board[1][2] == sign:
print(sign, "has Won.")
if board[2][0] == board[2][1] == board[2][2] == sign:
print(sign, "has Won.")
if board[0][0] == board[1][0] == board[2][0] == sign:
print(sign, "has Won.")
if board[0][1] == board[1][1] == board[2][1] == sign:
print(sign, "has Won.")
if board[0][2] == board[1][2] == board[2][2] == sign:
print(sign, "has Won.")
if board[0][0] == board[1][1] == board[2][2] == sign:
print(sign, "has Won.")
if board[0][2] == board[1][1] == board[2][0] == sign:
print(sign, "has Won.")
def draw_move(board):
while True:
row = randrange(3)
col = randrange(3)
if ([row],[col]) not in free_field: #Here where I call it but can't define it !
continue
else:
board[row][col] = 'X'
return
human = 'O'
computer = 'X'
sign = human or computer
display_board(board)
enter_move(board)
victory_for(board, sign)
draw_move(board)