为什么我会得到变量未定义的错误



以下是我的代码

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)

问题:

现在,我正在尝试制作一个可以转移数字的Python瓷砖游戏。Make Board功能显然创建了一个板,我做到了,以便在一个大列表中有三个列表,在三个列表中,有元素

,查找空白函数标识了董事会非现有部分的坐标

,查找图块函数是用户输入一个值的函数,代码标识了我们想要的瓷砖的坐标

因此,目前我正在遇到错误以下错误

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

我尝试将"瓷砖"变量变成一个全局,但根本没有起作用

find_next_to_blank(board)中未定义变量tiles。如果我不这样说,我会被遗忘:考虑重组您的程序以避免使用全局变量。

现在,如果您制作tiles全局,它应该起作用。

相关内容

  • 没有找到相关文章

最新更新