python - 类型错误:promovePeao() 缺少 1 个必需的位置参数: 'player'

标签 python typeerror

我有以下代码:

if(promovePeao(board,player) or (not getMovesPecas(board)['W'])):
   print("HELLO")

我的目的是检查棋盘最后一排是否有玩家

def promovePeao(self, board, player):
    if(player == 'B'):
        for a in (1,9):
            if(board[(1,a)] == player):
                return True
        return False
    else:
        for b in (1,9):
            if(board[(8,b)] == player):
                return True
        return False

但是当我执行代码时,我收到错误:

TypeError: promovePeao() missing 1 required positional argument: 'player'

有什么想法吗?

最佳答案

您像方法一样定义promovePeao,但像函数一样调用它。您收到错误是因为该函数需要 self 参数,但它不会自动传递,因为它没有在对象上调用。将其更改为:

def promovePeao(board, player): #Note that there is no self argument
    if(player == 'B'):
        for a in (1,9):
            if(board[(1,a)] == player):
                return True
        return False
    else:
        for b in (1,9):
            if(board[(8,b)] == player):
                return True
        return False

关于python - 类型错误:promovePeao() 缺少 1 个必需的位置参数: 'player',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53823308/

相关文章:

python - 如何从任意长度的列表中获取列?

Python 优化 - 调用迭代函数会显着减慢我的程序速度吗?

php - 类型错误:$(...) 不是函数

javascript - AJAX 成功对象中未捕获类型错误

javascript - 未捕获的类型错误 : undefined is not a function for window. 位置.路径名

python - setuptools 在安装过程中发现错误的包

python - sqlalchemy,将 MySQL 字符集设置为 `create_engine` 参数

python - 如何使用 Twilio 调用出站 SIP 调用?

Python 类型错误 : 'list' object cannot be interpreted as an integer

json - 为什么我的 Django Rest Framework 端点的右括号松动?