Global variable issue

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) 

You aren’t actually calling the make_list_of_free_fields function,
so nothing inside it – including the creation of the global variable –
gets run.

You have this:

def draw_move(board):
    free_field =make_list_of_free_fields   **I can't call it here, Why??!**

but that isn’t calling the function, it is just mentioning its name.

Use this instead:

    free_field = make_list_of_free_fields(board)

By the way, I don’t see any reason why free_field has to be a global variable.

One last thing: you were probably getting an exception that would hint that there was something wrong with free_field:

>>> free_field = make_list_of_free_fields
>>> (1, 1) in free_field
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'function' is not iterable

When you ask a question related to an error, it is important that you copy and paste the full error, starting from the Traceback line, in your post.

2 Likes

First of all, I do appreciate your help :slight_smile:
second,

def draw_move(board):
    free_field =make_list_of_free_fields

Sorry for that part I was checking for any solution. the code i want your help with:

def draw_move(board):
    
    while True:
        row = randrange(3)
        col = randrange(3)

        if ([row],[col]) not in free_field:   <<<<< Here is the position where i call the var as i need it here
            continue

        else:
            board[row][col] = 'X'
            return

As Steven said, free_field is created by make_list_of_free_fields, but you’re not calling make_list_of_free_fields, so free_field is never created.

2 Likes

This does not call make_list_of_free_fields. It just puts a reference
to the function itself in free_field. You need to call the function
for it to do its work:

 free_field = make_list_of_free_fields()

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes

I’ve edited the code to the one I’m asking for, Sorry the part you are talking about was a try to solve
not the code

I’ve edited the code to the one I’m asking for, Sorry the part you are talking about was a try to solve
not the code can you please see it again?

Does your code have the line:

free_field = make_list_of_free_fields()

? I can’t see it.

2 Likes

No, I put it in the code wrongly #was testing it.But my real code is now edited above with no

free_field = make_list_of_free_fields()

draw_move has this line:

if ([row],[col]) not in free_field:

It’s looking for something in free_field.

Where are you setting free_field?

In make_list_of_free_fields.

Are you calling make_list_of_free_fields?

No.

So when draw_move tries to look in free_field it says can’t find free_field. Because you never called make_list_of_free_fields to set it.

3 Likes

Your function requires an argument:

def make_list_of_free_fields(board):

If you are calling it without an argument, it will still fail:

free_field = make_list_of_free_fields()

You need to pass board as an argument.

Are you calling make_list_of_free_fields?

No.

OMG! Do you know that it has been 2 days to find out what’s going on with that code!!!

Thank you Mr.Steven, I do appreciate your help, Sir

as a python beginner, I want to say: Wish you the best in your life.

I don’t know how to send you a friend request or something like that but hope to find you
in my future obstacles.