Writing Prompts for Debugging Python Code
While programming, unexpected errors are common.
To resolve these errors, efficient debugging is necessary.
Debugging is the process of finding the errors
in code, analyzing the root cause, and fixing
them.
The goal of debugging is to eliminate bugs from the program.
In this session, we will introduce effective prompt writing techniques for debugging Python code using AI tools.
How to Debug Efficiently
The key to debugging is to reproduce the situation where the error occurs and narrow down the cause step by step.
Typically, this involves using print statements to output important variables and intermediate results to locate the error or using debugging tools to execute the code line by line while checking the state of variables.
However, in cases where the code is very complex, these debugging methods may be inefficient.
Rather than debugging line by line, it is more efficient to ask AI to analyze and fix the errors in the code.
Writing Prompts to Get Help from AI
When using AI to debug Python code, it is beneficial to provide the intent of the code and the error message generated in the Python runtime environment.
Debugging Code Errors
Below is a Python function that adds two numbers, but it results in an error when executed.
Please check the error message and fix the error.
### Python Code
def add_numbers(a, b):
return a + b
result = add_numbers(5, '10')
print("Result:", result)
### Error Message
TypeError: unsupported operand type(s) for +: 'int' and 'str'
By providing the intent of the code (a function that adds two numbers) and the error message (TypeError: unsupported operand type(s) for +: 'int' and 'str'
), the AI can identify and fix the error more quickly.
When There Is No Error but the Desired Result Is Not Achieved
Additionally, when debugging with AI, providing reproducible examples can be helpful.
Depending on the situation, specifying the program execution environment (e.g., Windows, MacOS, Python version) can also be beneficial.
### Instructions
Below is a Python code that sorts a given list in descending order.
The code execution results in None being displayed in the Result.
Please fix this to produce a function that returns the list sorted in descending order.
def sort_numbers(numbers):
return numbers.sort(reverse=True)
### Program Execution Environment
Windows, Python 3.8.5 Version
### Reproducible Example
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
def sort_numbers(numbers):
return numbers.sort(reverse=True)
result = sort_numbers(numbers)
print("Result:", result)
Providing the prompt with the code's intent, error message, program execution environment, and reproducible example allows the AI to correct the code more accurately.
Why sort_numbers Function Returns None
The sort()
method sorts the list in place and has no return value.
Thus, executing numbers.sort(reverse=True)
sorts the numbers
list in descending order, but the return value is None
.
To return the sorted list after sorting it in descending order, you should use the sorted()
function.
The sorted()
function does not change the original list but returns a new, sorted list.
The revised code is as follows:
def sort_numbers(numbers):
return sorted(numbers, reverse=True)
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
result = sort_numbers(numbers)
# Print the sorted list
print("Result:", result)
The revised code sorts the numbers
list in descending order, returns the sorted list, and assigns it to the result
variable.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.