Python棋盘游戏中的错误消息-定义全局变量

Python棋盘游戏中的错误消息-定义全局变量,python,error-handling,grid,Python,Error Handling,Grid,嘿,我正在用python做一个棋盘游戏,我遇到了一些错误 例如: ERROR_MOVE_CANT = "Error: %s %s can't move in direction %s" %(player, piece, direction) 当我稍后在程序中尝试提出此错误时,我使用: if board[newch[1]][newch[0]] != '.': return ((ERROR_MOVE_CANT)%(player, piece, direction)) 我收到一个错误“Na

嘿,我正在用python做一个棋盘游戏,我遇到了一些错误

例如:

ERROR_MOVE_CANT = "Error: %s %s can't move in direction %s" %(player, piece, direction)
当我稍后在程序中尝试提出此错误时,我使用:

if board[newch[1]][newch[0]] != '.':
    return ((ERROR_MOVE_CANT)%(player, piece, direction))
我收到一个错误“NameError:未定义全局名称‘player’”


我之前已经将“玩家”定义为“字母”或“数字”,但如何将其定义为全局变量?

全局只是最后一个查看的位置;您可能希望在使用时将其设置为局部变量,或者如果它是实例变量,则正确引用它:
self.player

您可能在函数中设置了错误消息,创建了局部变量而不是全局变量:

def f():
    ERROR = 17
    # this variable is local to f

def g():
    global ERROR
    ERROR = 17
    #now the ERROR in this function is the global one.


(顺便说一句,在使用全局变量之前要三思而后行,它们很可能不是解决问题的最干净的解决方案。)

您应该删除所有的
并使用代码格式化选项({}按钮;)