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 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.
Issue: Missing colon after function definition and not actually calling the function.
def greet():
print("Hello, World!")
greet()
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.
Issue: Missing comma between parameters.
def sum_numbers(a, b):
print(a + b)
sum_numbers(3, 4)
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.
Issue: Variable 'message' is undefined inside the function.
def say_hello():
message = "Hello!"
print(message)
say_hello()
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.
Issue: The function calculates the sum but does not return it.
def add_numbers(a, b):
total = a + b
return total
result = add_numbers(3, 4)
print(result)
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.
Issue: Function is not being called; it's only referenced.
def greet(name="User"):
print("Hello, " + name)
greet()
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.
Issue: Incorrect variable name 'num' instead of 'nums'.
def average(nums):
return sum(nums) / len(nums)
print(average([1, 2, 3]))
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.
Issue: Missing '*' in the function parameter for variable-length arguments.
def print_all(*args):
for item in args:
print(item)
print_all(1, 2, 3)
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.
Issue: Missing 'for' keyword in the list comprehension.
def squares(numbers):
return [n ** 2 for n in numbers]
print(squares([1, 2, 3]))
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.
Issue: Used '=' instead of '==' in the if condition.
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
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.
Issue: Passing the dictionary directly instead of unpacking it.
def print_info(name, age):
print(f"Name: {name}, Age: {age}")
info = {"name": "Alice", "age": 25}
print_info(**info)
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.
Issue: Using a mutable default argument causes shared state between calls.
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))
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.
Issue: Division by zero raises a ZeroDivisionError.
def divide(a, b):
try:
return a / b
except ZeroDivisionError:
return "Cannot divide by zero"
print(divide(5, 0))
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'.
Issue: Using 'return' inside a lambda function is invalid.
square = lambda x: x ** 2
print(square(4))
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.
Issue: Trying to modify a global variable without declaring it global.
count = 0
def increment():
global count
count += 1
increment()
print(count)
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.
Issue: Using lst[2:] instead of lst[1:], which skips every other element.
def recursive_sum(lst):
if len(lst) == 0:
return 0
return lst[0] + recursive_sum(lst[1:])
print(recursive_sum([1, 2, 3, 4]))
Tutorials, Roadmaps, Bootcamps & Visualization Projects