Fix Python Bug
By Dmitri Molokov
Fix Python Bug
Simple bug-finding task for a Python programmer:
Task: Find the Bug in the Code Here is a short Python function that is supposed to return the square of a number, but it contains a bug. Your job is to find and fix the bug.
def square_number(num):
result = num * num
return result + 1
print(square_number(5)) # Expected output: 25
What’s wrong with this code? How can you fix it?
Original code:
def square_number(num):
result = num * num
return result + 1
print(square_number(5)) # Expected output: 25
What’s wrong? The function is supposed to return the square of the number num. Squaring means multiplying the number by itself, which is done correctly here by num * num. However, after computing the square, the function adds 1 to the result (return result + 1).
Explanation:
So when you call square_number(5):
It calculates 5 * 5 = 25
Then adds 1, giving 26
But the expected output is 25
How to fix it? Simply remove the + 1 part from the return statement so the function returns the correct squared value:
def square_number(num):
result = num * num
return result
print(square_number(5)) # Output: 25
Here are a few more simple bug-finding practice tasks for you:
Task 1: Find the Bug in the Average Function
The following function is intended to calculate the average of two numbers, but it contains a bug.
def average(a, b):
return a + b / 2
print(average(4, 6)) # Expected output: 5
Find and fix the bug.
Task 2: List Index Error
This function tries to access the last item in a list, but there’s a bug.
def get_last_element(lst):
return lst[len(lst)]
print(get_last_element([1, 2, 3])) # Expected output: 3
Find and fix the bug.
Task 3: String Concatenation Issue Fix the bug so the function returns “Hello, World!”
def greet(name):
return "Hello, " + name + "!"
print(greet(Hello, World))
def get_last_element(lst):
return lst[len(lst)]
print(get_last_element([1, 2, 3])) # Expected output: 3
Find and fix the bug.
Task 3: String Concatenation Issue
Fix the bug so the function returns “Hello, World!”
def greet(name):
return "Hello, " + name + "!"
print(greet("World"))
def greet(name):
return "Hello, " + name + "!"
print(greet(Hello, World))
Find and fix the bug.
Let’s go through the solutions for the three practice tasks I gave you:
Task 1: Find the Bug in the Average Function
Original code:
def average(a, b):
return a + b / 2
print(average(4, 6)) # Expected output: 5
What’s wrong?
The function attempts to calculate the average of two numbers by computing a + b / 2. Due to operator precedence, this computes:
b / 2 first
then adds a
So for average(4, 6) it performs: 4 + (6 / 2) = 4 + 3 = 7
The expected output is 5 (since (4 + 6) / 2 = 5), so the calculation is incorrect.
How to fix it?
You need to add parentheses to ensure a + b is summed first, then divided by 2:
def average(a, b):
return (a + b) / 2
print(average(4, 6)) # Output: 5
Task 2: List Index Error
Original code:
def get_last_element(lst):
return lst[len(lst)]
print(get_last_element([1, 2, 3])) # Expected output: 3
What’s wrong? List indices in Python are zero-based. len(lst) returns the length of the list, but valid indices range from 0 to len(lst) - 1.
For [1,len(lst)
is 3, but valid indices are 0, 1, 2.
lst[len(lst)]
tries to access lst, which raises an IndexError.
How to fix it? Use index len(lst) - 1 to get the last element:
def get_last_element(lst):
return lst[len(lst) - 1]
print(get_last_element([1, 2, 3])) # Output: 3
Alternatively, you can use negative indexing:
def get_last_element(lst):
return lst[-1]
print(get_last_element([1, 2, 3])) # Output: 3
Task 3: String Concatenation Issue
Original code:
def greet(name):
return "Hello, " + name + "!"
def get_last_element(lst):
return lst[-1]
print(get_last_element([1, 2, 3])) # Output: 3
Task 3: String Concatenation Issue
Original code:
def greet(name):
return "Hello, " + name + "!"
print(greet(Hello, World))
What’s wrong? In the print statement, greet(Hello, World) treats Hello and World as variables or identifiers instead of a single string argument. This will raise a NameError unless these variables are defined earlier.
Additionally, the function expects one argument (name), but two are given due to the comma.
How to fix it? Pass the argument as a single string (in quotes):
def greet(name):
return "Hello, " + name + "!"
print(greet("World")) # Output: Hello, World!
or if you want to greet “Hello, World!” specifically:
def greet(name):
return "Hello, " + name + "!"
print(greet("Hello, World!"))
print(greet("World"))
Feel free to leave feedback I will always appreciate it!