【问题标题】:Why am I getting the variable not defined error?为什么我收到变量未定义错误?
【发布时间】:2017-12-01 02:20:37
【问题描述】:

以下是我的代码

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 Tile 游戏,我可以在其中移动数字。 make board 函数显然创建了一个板,我这样做是为了在一个大列表中有三个列表,并且在三个列表中有元素

并且查找空白功能识别板的不存在部分的坐标

find tile函数是用户输入一个值,代码识别出我们想要的tile的坐标是什么

所以目前我收到一个错误,因为当我运行 find next to blank 函数时(该函数应该识别用户想要输入的值旁边是否有空白点)我得到以下错误

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

我尝试将“tiles”变量变成一个全局变量,但它根本不起作用

【问题讨论】:

  • find_next_to_blank方法范围内没有变量'tiles',是否需要作为find_next_to_blank方法中的参数?
  • 尝试使瓷砖全球化似乎是一个不错的方向。您应该显示您尝试的代码作为起点。
  • 格式太差了,我帮你编辑了,下次注意了。

标签: python list variables pygame global-variables


【解决方案1】:

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

现在不碍事了,如果你将 tiles 设为全局,它应该可以工作。

【讨论】:

    猜你喜欢
    • 2014-04-23
    • 2020-09-28
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多