简体   繁体   中英

Why am I getting the variable not defined error?

The following is my code

n = int(input("please enter a value: "))
board = []

def make_board(n):
    global board
    max = n * n #the number of tiles in the board
    count = 1 #a counter to change the value assigned to each tile
    for i in range(n):
        board.append([]) #appends a list inside the list of board.  Essentially creates a row which is of type list.
        for j in range(n): 
            num = max - count 
            if num == 0: #the 0 tile will display like a blank space
                tile = '  '
            elif num < 10: #adds a space to tile values less than 10 for formatting.
                tile = ' ' + str(num)
            else:
                tile = str(num) 
            board[i].append(tile) #appends a tile value to each row, n number of times.
            count += 1

    if n%2 == 0:
        tempvara = board[n-1][n-2]
        tempvarb = board[n-1][n-3]
        board[n-1][n-2]=tempvarb
        board[n-1][n-3]=tempvara
    #TODO
    for row in board:
        print(' '.join(row))

def find_blank(board):
    global counterUNO
    global counterDOS
    global counterTRES
    counterTRES = 0

    #TODO
    for i in range(n):
            tempvari = board[i]
            if '  ' in tempvari:
                counterUNO = i
                for z in board[counterUNO]:
                    counterTRES = counterTRES + 1
                    if '  ' in z:
                        counterDOS = counterTRES-1
                        break

    tupleone = (counterUNO,counterDOS)
    return(tupleone)

def find_tile(f):
    counterfour = 0
    tiles = str(input("tile BUBBER"))

    if int(tiles)<10:
        tiles = " "+tiles

    counterfive = 0
    countersixe = 0
    countersixe = 0

    for i in range(n):
        chopstick = board[i]
        if tiles in chopstick:
            counterfour = i
            for z in board[counterfour]:
                countersixe = countersixe + 1
                if tiles in z:
                    counterfive = countersixe-1
                    break

    tupleDOS = (counterfour,counterfive)
    return(tupleDOS)

def find_next_to_blank(board):
    #print("here is the shit")
    #print(find_tile(board))
    vara  = find_tile(board) #sets tile numb tuple to vara
    varb  = find_blank(board) #yeah
    varc  = int(tiles)
    varaa = int(tiles[0])
    varab = int(tiles[1])
    varba = board[varaa+1][varab]
    varbb = board[varaa][varab+1]
    varbc = board[varaa-1][varab]
    varbd = board[varaa][varab-1]
    varbe = board[varaa+1][varab+1]
    varbf = board[varaa-1][varab-1]

make_board(n)    
#find_blank(board)            
#find_tile(board)
find_next_to_blank(board)

Problem:

Right now I am trying to make a python Tile game where I can shift the numbers. the make board function obviously creates a board, i did it so that there are three lists in a big list and in the three list there are elements

and the find blank function identifies the coordinate of the non existing section of the board

and the find tile function is the function that the user inputs a value and the code identifies what is the coordinate of the tile that we want

So currently I am getting an error because when i am running the find next to blank function (the function is supposed to identify whether or not there is a blank spot next to the value which the user wants to input) i get the following error

Traceback (most recent call last):
  File "python", line 149, in <module>
  File "python", line 122, in find_next_to_blank
NameError: name 'tiles' is not defined

and i tried making "tiles" variable into a global one but it didn't work at all

The variable tiles is not defined in find_next_to_blank(board) . I'd be remiss if I don't say this: consider restructuring your program to avoid using global variables.

Now that's out of the way, if you make tiles global, it ought to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM