gpt4 book ai didi

python - 语法错误 : positional argument follows keyword argument:

转载 作者:行者123 更新时间:2023-12-03 15:16:46 40 4
gpt4 key购买 nike

所以这是我的代码:

    def is_valid_move(board, column):
'''Returns True if and only if there is an open cell in column'''
for i in board[col]:
if i == 1 or i == 2:
return False
else:
return True
然后我尝试使用以下方法测试我的功能:
    print(is_valid_move(board = [[2, 2, 0, 2, 2, 2, 2], 
[1, 2, 2, 2, 2, 2, 2],
[1, 1, 2, 2, 1, 2, 1],
[1, 1, 2, 2, 1, 2, 1],
[1, 1, 2, 2, 1, 2, 1],
[1, 1, 2, 2, 1, 2, 1]],
2))
我以前从未遇到过此错误,因此我对如何实际解决此问题或这意味着什么感到有些困惑。

最佳答案

有两种类型的参数:位置参数和关键字参数。

如果我们有函数:

def f(a, b):
return a + b

然后我们可以使用位置参数调用它:
f(4, 4)
# 8

或关键字参数:
f(a=4, b=4)
# 8

但不是在 order 关键字 --> positional 中,这就是您正在做的事情:
f(a=4, 4)
# SyntaxError: positional argument follows keyword argument
f(4, b=4)
# 8

这是有原因的。再一次,假设我们有一个类似的函数:
def f(a, b, *args):
return a + b + sum(args)

我们怎么知道在调用这个函数时什么参数是 a ,什么参数是 b ,以及 args 的用途?

关于python - 语法错误 : positional argument follows keyword argument:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53605836/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com