How to Fix: SyntaxError: positional argument follows keyword argument

3D illustration of a labeled box blocking an unlabeled box on a conveyor belt, representing the positional argument syntax error.

This SyntaxError positional argument is a simple but strict rule about how you call functions in Python.

The Rule: You can pass arguments in two ways:

  1. Positional: By position ("Alice", 30).
  2. Keyword: By name (job="Engineer").

When you call a function, all positional arguments must come before all keyword arguments.

The Cause

You passed a keyword argument, and then tried to pass a positional argument after it.

Problem Code: Let’s say we have this function:

def create_user(name, age, job):
    print(f"User: {name}, Age: {age}, Job: {job}")

Now we call it, but we mix up the order:

# 1. Positional (OK)
# 2. Keyword (OK)
# 3. Positional (NOT OK!)
create_user("Alice", age=30, "Engineer")
# CRASH! SyntaxError: positional argument follows keyword argument

Python sees "Engineer" and thinks, “I’m already past the positional part. I don’t know where this one goes!”

The Fixes

Fix 1: Make it a Keyword Argument

The easiest fix is to just give the last argument its name.

# All keyword arguments
create_user(name="Alice", age=30, job="Engineer")

Fix 2: Reorder Your Arguments

Move all your positional arguments to the front.

# All positional arguments
create_user("Alice", 30, "Engineer")

# A valid mix
create_user("Alice", 30, job="Engineer")

Similar Posts

Leave a Reply