Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error: positional argument follows keyword argument:

Tags:

python

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.

like image 645
Stevo Avatar asked Dec 04 '18 04:12

Stevo


People also ask

What does SyntaxError positional argument follows keyword argument?

The Python SyntaxError: positional argument follows keyword argument occurs when you try to specify a positional argument after a keyword argument in a function call.

Can keyword arguments be followed by positional arguments Python?

Keyword arguments aren't just useful for functions that accept any number of positional arguments (like print ). You can pass keyword arguments to just about any function in Python. We're passing in one positional argument and one keyword argument.

What is positional argument and keyword argument in Python?

A positional argument means its position matters in a function call. A keyword argument is a function argument with a name label. Passing arguments as keyword arguments means order does not matter.

What is positional arguments in Python with example?

An example of positional arguments can be seen in Python's complex() function. This function returns a complex number with a real term and an imaginary term. The order that numbers are passed to the complex() function determines which number is the real term and which number is the imaginary term.


1 Answers

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?

like image 137
TerryA Avatar answered Sep 20 '22 03:09

TerryA