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

标签 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))

我收到错误"NameError: global name 'player' is not defined"
我之前已将“玩家”定义为“字母”或“数字”,但是如何将其定义为全局变量?

最佳答案

全局只是看的最后一个地方;您可能想在使用时将其设为局部变量,或者如果它是实例变量self.player,则可以正确地引用它。

关于python - Python棋盘游戏中的错误消息-定义全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173599/

相关文章:

python - 为 Python REST API 函数编写单元测试

python - Create_Index 在 python 中使用 partialFilterExpression

javascript - 使用 JavaScript 从网格数组中删除列

error-handling - 系统找不到在SublimeText3中指定的用于lua构建的文件。我该如何解决?

web-scraping - 如何人为地创建ConnectionRefusedError?

html - 使用 flexbox 定位网格

ExtJS 4 : grid: how to select a newly created record?

python - Google Firebase : Get, 使用 Python 更新或创建文档

javascript - 打开页面时执行 JavaScript

error-handling - Play中Controller.flash的替代品! 1.2.4