简体   繁体   English

语法错误:位置参数跟随关键字参数:

[英]Syntax Error: positional argument follows keyword argument:

so here is my code: 所以这是我的代码:

    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

and then I'm trying to test my function using: 然后我尝试使用以下方法测试我的功能:

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

I've never gotten this error before so i'm a bit confused on how to actually fix this, or what this even means. 我从来没有遇到过这个错误,所以我对如何真正解决这个问题甚至这意味着什么感到困惑。

There are two types of arguments: positional and keyword. 参数有两种类型:位置参数和关键字。

If we have the function: 如果我们具有以下功能:

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

Then we can call it with positional arguments: 然后我们可以使用位置参数来调用它:

f(4, 4)
# 8

Or keyword arguments: 或关键字参数:

f(a=4, b=4)
# 8

But not both in the order keyword --> positional, which is what you're doing: 但是您不能在命令关键字->位置关键字中同时使用这两个关键字:

f(a=4, 4)
# SyntaxError: positional argument follows keyword argument
f(4, b=4)
# 8

There's a reason why this is so. 这是有原因的。 Again, imagine we have a similar function: 再次,假设我们有一个类似的功能:

def f(a, b, *args):
    return a + b + sum(args)

How would we know when calling this function what argument is a , what argument is b , and what is for args ? 当调用此函数时,我们如何知道什么参数是a ,什么参数是b以及什么是args

Keyword argument should follow non-keyword arguments in function invocation. 在函数调用中,关键字参数应遵循非关键字参数。 In your case, you should assign board to a variable and pass this variable to function. 在您的情况下,应将电路板分配给一个变量,然后将此变量传递给功能。

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]]
print(is_valid_move(board, 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PYTHON中的错误位置参数跟随关键字参数 - ERROR IN PYTHON Positional argument follows keyword argument 位置参数跟在关键字参数之后| 调用函数时出错 - Positional argument follows keyword argument | Error while calling function 未能更正:此错误 SyntaxError:位置参数跟随关键字参数 - failed to correct : this error SyntaxError: positional argument follows keyword argument 在决策树可视化过程中,位置参数遵循关键字参数错误 - Positional argument follows keyword argument error during decision tree visualisation 位置参数跟随关键字参数当我尝试分组时出错 - positional argument follows keyword argument Error when i try to group by Python-位置参数紧跟关键字参数 - Python- positional argument follows keyword argument 什么是关键字关键字后面的位置参数? - What is positional argument follows keyword argument? SyntaxError:位置参数跟随关键字参数 - SyntaxError: positional argument follows keyword argument 位置参数跟随带破折号python的关键字参数图形 - positional argument follows keyword argument graphics with dash python 语法错误:位置参数跟随关键字参数 [discord.py] - SyntaxError: positional argument follows keyword argument [discord.py]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM