TypeError:promovePeao()缺少1个必需的位置参数:'player'[重复] - python

This question already has answers here:

TypeError: Missing 1 required positional argument: 'self'
                                
                                    (5个答案)
                                
                        
                                2年前关闭。
            
                    
我有以下代码:

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'

任何想法为何?

python参考方案

您将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

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …

如何根据子列表的长度对列表列表进行排序[重复] - python

This question already has answers here: Sorting Python list based on the length of the string (7个答案) 5年前关闭。 我有以下清单a = [['a', 'b', 'c'], ['d'…

为随机选择的变量分配一个值[重复] - python

This question already has answers here: How do I randomly select a variable from a list, and then modify it in python?                                                              …

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…