Python SyntaxError: positional argument follows keyword argument Solution

Posted in /  

Python SyntaxError: positional argument follows keyword argument Solution
vinaykhatri

Vinay Khatri
Last updated on April 19, 2024

    Python support two types of argument passing during a function call, Positional and Keyword Arguments. In Positional arguments, we do not specify the parameter names, and the argument must appear in the particular order as the parameter defined in the Function definition.

    For example

    def fun(para1, para2):
        pass
    
    fun(arg1, arg2)

    But in Keyword arguments, we can specify the parameter name and its value during the function call.  And here, it is not necessary to maintain the argument order. For example

    def fun(para1, para2):
        pass
    
    fun(para2= arg2, para1= arg1)

    We can also specify the arguments as a mixer of positional and keyword arguments, but there the positional argument needs to be specified before the keyword argument. Otherwise, we will receive the SyntaxError: positional argument follows keyword argument Error. In this Python guide, we will learn what is SyntaxError: positional argument follows keyword argument Error in Python and how to debug it. So let's get started with the error statement.

    Python Error: SyntaxError: positional argument follows keyword argument

    The Error Statement SyntaxError: positional argument follows keyword argument is divided into two parts:

    1. Python Exception
    2. Error Message

    1. Python Exception ( SyntaxError )

    Syntax Error is raised in a Python program when we write an invalid code structure that the Python interpreter could not understand. Misspelled keywords, empty blocks, putting a keyword in a worn place, and leaving commas between elements are some of the common SyntaxError exceptions.

    2. Error Message( positional argument follows keyword argument )

    We receive this error message when we try to pass a keyword argument before a positional argument while calling a function.

    Error Example

    According to the Python Syntax, when we use the Positional and Keyword arguments simultaneously while calling a function. The positional argument must be specified before the Keyword argument. So, Python can first map the Positional arguments with the Parameters according to their order, and if there is a keyword argument, it could be mapped according to the Parameter name.

    Let's create an example where we violate this Syntax of Python and try to pass the Keyword argument before the positional argument.

    Example

    names = ["Rahul", "Ravi", "Rajesh"]
    medals  = ["Gold", "Silver", "Bronze"]
    
    
    def score_board(names,  medals):
        ranks=[1,2,3]
        for name, medal, rank in zip(names, medals, ranks):
            print(rank, name,"------>", medal)
    
    score_board(names=names, medals)

    Output

     File "main.py", line 11
    score_board(names=names, medals)
    ^
    SyntaxError: positional argument follows keyword argument

    Break the code

    In the above example, we are getting the SyntaxError: positional argument follows keyword argument at line 11 with the statement score_board(names=names, medals) . This is because, at function call, we are specifying the keyword argument names=names before the positional argument medals .

    If we look at the function calling statement, the order of arguments is still maintained, the names argument is still before the medals argument. But when we use the mixture of positional and keyword arguments, we also need to make sure that the positional argument must be specified first, then the keyword arguments.

    Solution

    In the above problem, we only have two argument values, which means we can either make them positional or keyword, and both will do the trick for us. But here, we are trying to use the combination of Positional and Keyword. The right solution for our above example would be to make the name argument positional and medals argument keyword.

    Example Solution

    names = ["Rahul", "Ravi", "Rajesh"]
    medals  = ["Gold", "Silver", "Bronze"]
    
    
    def score_board(names,  medals):
        ranks=[1,2,3]
        for name, medal, rank in zip(names, medals, ranks):
            print(rank, name,"------>", medal)
    
    score_board(names,medals= medals)

    Output

    1 Rahul ------> Gold
    2 Ravi ------> Silver
    3 Rajesh ------> Bronze

    Wrapping Up!

    In Python, when we call a function, we either use positional arguments or Keyword Arguments. In method calls, we generally use Keyword arguments, and we use positional arguments for the user-defined function. You will rarely be using both argument types at once. Still, you should know that the positional argument must be positioned before the keyword arguments, else you will encounter the SyntaxError: positional argument follows keyword argument Error.

    If you are stuck with this Python error, you can share your code in the comment section, we will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments