πŸ”§ Error Fixes
Β· 1 min read

Python TypeError: Takes X Positional Arguments but Y Were Given β€” How to Fix It


TypeError: function() takes 2 positional arguments but 3 were given

You’re passing more (or fewer) arguments to a function than it expects.

Fix 1: Wrong number of arguments

def greet(name):
    print(f"Hello, {name}")

# ❌ Too many arguments
greet("Alice", "Bob")  # TypeError!

# βœ… Correct
greet("Alice")

Fix 2: Forgot self in a class method

class User:
    # ❌ Missing self β€” Python passes it automatically
    def greet(name):
        print(f"Hello, {name}")

user = User()
user.greet()  # TypeError: takes 1 but 2 were given

# βœ… Add self
class User:
    def greet(self, name):
        print(f"Hello, {name}")

When you call user.greet(), Python automatically passes user as the first argument (self).

Fix 3: Passing a list instead of unpacking

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

numbers = [3, 5]

# ❌ Passing one list argument
add(numbers)  # TypeError!

# βœ… Unpack with *
add(*numbers)  # 8

Related: Pip Install Error Fix

πŸ“˜