简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM