Home > Mobile >  python beginner - function can't see variable array assigned outside the function - UnboundLoca
python beginner - function can't see variable array assigned outside the function - UnboundLoca

Time:10-13

Netbie programmer learning in Edube doing tic-tac-toe project.

Still building the code but part stuck on is when testing to assign [12] (instead of X or O just yet) to list array n defined outside the function(s), the def display_board function sees the array n, but def select_move does not see n getting unboundlocalerror: local variable 'n' referenced before assignment. Why can the 1st function see n but not the 2nd function?

I tried making global n inside and outside function. didn't work outside. when done inside error stops but new assignment doesn't work and my understanding assigning global within the function makes the variable unchangable which defeats the point. (need to be able to update the variable n array from 0-8 to X's and O's as game progresses).

Please thank you for suggestions.

n =[[0,1,2],
    [3,4,5],
    [6,7,8]]


def display_board(board):
    for x in range(9):
        if x == 1:
            print("|  ",n[0][0],"  |  ",n[0][1],"  |  ",n[0][2],"  |")
        elif x == 4:
            print("|  ",n[1][0],"  |  ",n[1][1],"  |  ",n[1][2],"  |")
        elif x == 7:
            print("|  ",n[2][0],"  |  ",n[2][1],"  |  ",n[2][2],"  |")
        else:
            print("|       |       |       |")
                
print (display_board(0))


move = 0
def select_move(move):
    
    print (input("Select a spot: "))
    if move in n:
        n = [12]
    print (display_board(0))   
select_move(move)
-----------------------------------------------------------------

|       |       |       |
|   0   |   1   |   2   |
|       |       |       |
|       |       |       |
|   3   |   4   |   5   |
|       |       |       |
|       |       |       |
|   6   |   7   |   8   |
|       |       |       |
None
Select a spot: 6
6
Traceback (most recent call last):
  File "main.py", line 28, in <module>
    select_move(move)
  File "main.py", line 25, in select_move
    if move in n:
UnboundLocalError: local variable 'n' referenced before assignment

CodePudding user response:

declare global variable in function

n =[[0,1,2],
    [3,4,5],
    [6,7,8]]


def display_board(board):
    for x in range(9):
        if x == 1:
            print("|  ",n[0][0],"  |  ",n[0][1],"  |  ",n[0][2],"  |")
        elif x == 4:
            print("|  ",n[1][0],"  |  ",n[1][1],"  |  ",n[1][2],"  |")
        elif x == 7:
            print("|  ",n[2][0],"  |  ",n[2][1],"  |  ",n[2][2],"  |")
        else:
            print("|       |       |       |")
                
print (display_board(0))


move = 0
def select_move(move):
    global n
    print (input("Select a spot: "))
    if move in n:
        n = [12]
    print (display_board(0))   
select_move(move)

CodePudding user response:

for your mentioned error ''unboundlocalerror: local variable 'n' referenced before assignment'' - you can pass the 'n' to the function 'select_move(move,n)' to solve this error. As you mentioned that you are still building the code, complete your code building for the problem then check once it works. All the best!!!

n =[[0,1,2],
[3,4,5],
[6,7,8]]


def display_board(board):
   for x in range(9):
       if x == 1:
          print("|  ",n[0][0],"  |  ",n[0][1],"  |  ",n[0][2],"  |")
      elif x == 4:
          print("|  ",n[1][0],"  |  ",n[1][1],"  |  ",n[1][2],"  |")
      elif x == 7:
          print("|  ",n[2][0],"  |  ",n[2][1],"  |  ",n[2][2],"  |")
      else:
          print("|       |       |       |")
            
print (display_board(0))


move = 0
def select_move(move,n):
    print (input("Select a spot: "))
    if move in n:
      n = [12]
    print (display_board(0))   
select_move(move,n)
  • Related