Learn with Yasir

Share Your Feedback

Python functions Debugging – Find and Fix Mistakes Exercises for Beginners

Improve your Python skills by finding and fixing common mistakes in functions code. Practice debugging set operations, membership, and manipulation with beginner-friendly exercises and solutions.

Topic: functions


🧪 Fix & Find Questions

🟢 Beginner Fix & Find

  1. Fix the code so that the function prints "Hello, World!" when called.

    def greet()
        print("Hello, World!")
    greet
    

    Hint: 💡 Check the function definition syntax and how it's being called.

    🔍 View Issue & Fixed Solution

    Issue: Missing colon after function definition and not actually calling the function.

    ✅ Fixed Solution

    def greet():
        print("Hello, World!")
    greet()
    

  2. Fix the code so that the function takes two parameters and prints their sum.

    def sum_numbers(a b):
        print(a + b)
    sum_numbers(3, 4)
    

    Hint: 💡 Check the parameter list syntax.

    🔍 View Issue & Fixed Solution

    Issue: Missing comma between parameters.

    ✅ Fixed Solution

    def sum_numbers(a, b):
        print(a + b)
    sum_numbers(3, 4)
    

  3. Fix the code so that it doesn't throw a NameError.

    def say_hello():
        print(message)
    say_hello()
    

    Hint: 💡 The variable used inside the function must be defined or passed as a parameter.

    🔍 View Issue & Fixed Solution

    Issue: Variable 'message' is undefined inside the function.

    ✅ Fixed Solution

    def say_hello():
        message = "Hello!"
        print(message)
    say_hello()
    

🟡 Intermediate Fix & Find

  1. Fix the code so that the function returns the sum of two numbers instead of None.

    def add_numbers(a, b):
        total = a + b
    result = add_numbers(3, 4)
    print(result)
    

    Hint: 💡 Make sure the function uses the 'return' statement.

    🔍 View Issue & Fixed Solution

    Issue: The function calculates the sum but does not return it.

    ✅ Fixed Solution

    def add_numbers(a, b):
        total = a + b
        return total
    result = add_numbers(3, 4)
    print(result)
    

  2. Fix the code so that it correctly uses a default parameter for the greeting.

    def greet(name = "User"):
        print("Hello, " + name)
    greet
    

    Hint: 💡 Check the function call — it's missing parentheses.

    🔍 View Issue & Fixed Solution

    Issue: Function is not being called; it's only referenced.

    ✅ Fixed Solution

    def greet(name="User"):
        print("Hello, " + name)
    greet()
    

  3. Fix the code so that it returns the correct average of a list of numbers.

    def average(nums):
        return sum(nums) / len(num)
    print(average([1, 2, 3]))
    

    Hint: 💡 Check the variable name in the division part.

    🔍 View Issue & Fixed Solution

    Issue: Incorrect variable name 'num' instead of 'nums'.

    ✅ Fixed Solution

    def average(nums):
        return sum(nums) / len(nums)
    print(average([1, 2, 3]))
    

  4. Fix the code so that it accepts any number of arguments.

    def print_all(args):
        for item in args:
            print(item)
    print_all(1, 2, 3)
    

    Hint: 💡 Use the correct syntax for variable-length arguments.

    🔍 View Issue & Fixed Solution

    Issue: Missing '*' in the function parameter for variable-length arguments.

    ✅ Fixed Solution

    def print_all(*args):
        for item in args:
            print(item)
    print_all(1, 2, 3)
    

  5. Fix the code so that it calculates the square of each number in a list.

    def squares(numbers):
        return [n ** 2 numbers]
    print(squares([1, 2, 3]))
    

    Hint: 💡 Check the list comprehension syntax.

    🔍 View Issue & Fixed Solution

    Issue: Missing 'for' keyword in the list comprehension.

    ✅ Fixed Solution

    def squares(numbers):
        return [n ** 2 for n in numbers]
    print(squares([1, 2, 3]))
    

🔴 Advanced Fix & Find

  1. Fix the code so that the recursive factorial function works without errors.

    def factorial(n):
        if n = 1:
            return 1
        else:
            return n * factorial(n - 1)
    print(factorial(5))
    

    Hint: 💡 Check the conditional statement syntax.

    🔍 View Issue & Fixed Solution

    Issue: Used '=' instead of '==' in the if condition.

    ✅ Fixed Solution

    def factorial(n):
        if n == 1:
            return 1
        else:
            return n * factorial(n - 1)
    print(factorial(5))
    

  2. Fix the code so that it correctly unpacks keyword arguments.

    def print_info(name, age):
        print(f"Name: {name}, Age: {age}")
    info = {"name": "Alice", "age": 25}
    print_info(info)
    

    Hint: 💡 Check how to pass dictionary values to a function.

    🔍 View Issue & Fixed Solution

    Issue: Passing the dictionary directly instead of unpacking it.

    ✅ Fixed Solution

    def print_info(name, age):
        print(f"Name: {name}, Age: {age}")
    info = {"name": "Alice", "age": 25}
    print_info(**info)
    

  3. Fix the code so that it doesn't cause a TypeError when using default mutable arguments.

    def append_item(item, my_list=[]):
        my_list.append(item)
        return my_list
    print(append_item(1))
    print(append_item(2))
    

    Hint: 💡 Default mutable arguments can cause unexpected behavior.

    🔍 View Issue & Fixed Solution

    Issue: Using a mutable default argument causes shared state between calls.

    ✅ Fixed Solution

    def append_item(item, my_list=None):
        if my_list is None:
            my_list = []
        my_list.append(item)
        return my_list
    print(append_item(1))
    print(append_item(2))
    

  4. Fix the code so that it can handle division by zero without crashing.

    def divide(a, b):
        return a / b
    print(divide(5, 0))
    

    Hint: 💡 Use exception handling for division by zero.

    🔍 View Issue & Fixed Solution

    Issue: Division by zero raises a ZeroDivisionError.

    ✅ Fixed Solution

    def divide(a, b):
        try:
            return a / b
        except ZeroDivisionError:
            return "Cannot divide by zero"
    print(divide(5, 0))
    

  5. Fix the code so that the lambda function works correctly for squaring numbers.

    square = lambda x: return x ** 2
    print(square(4))
    

    Hint: 💡 Lambda functions cannot contain 'return'.

    🔍 View Issue & Fixed Solution

    Issue: Using 'return' inside a lambda function is invalid.

    ✅ Fixed Solution

    square = lambda x: x ** 2
    print(square(4))
    

  6. Fix the code so that the function correctly modifies a global variable.

    count = 0
    def increment():
        count += 1
    increment()
    print(count)
    

    Hint: 💡 Use the 'global' keyword when modifying global variables inside a function.

    🔍 View Issue & Fixed Solution

    Issue: Trying to modify a global variable without declaring it global.

    ✅ Fixed Solution

    count = 0
    def increment():
        global count
        count += 1
    increment()
    print(count)
    

  7. Fix the code so that it uses recursion correctly to sum a list.

    def recursive_sum(lst):
        if len(lst) == 0:
            return 0
        return lst[0] + recursive_sum(lst[2:])
    print(recursive_sum([1, 2, 3, 4]))
    

    Hint: 💡 Check the slicing — it skips elements incorrectly.

    🔍 View Issue & Fixed Solution

    Issue: Using lst[2:] instead of lst[1:], which skips every other element.

    ✅ Fixed Solution

    def recursive_sum(lst):
        if len(lst) == 0:
            return 0
        return lst[0] + recursive_sum(lst[1:])
    print(recursive_sum([1, 2, 3, 4]))
    

📚 Related Resources

🧠 Practice & Progress

Explore More Topics

📘 Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules