Debug Python Code
Debug Python Code
Face up to bugs
Bugs are inevitable
As a beginner, you will find it soon that creating easy-to-use software is never easy. A bug or bugs would have occurred when software doesn’t work properly.
As a matter of fact, professional software developers may develop software with bugs as beginners do. The only difference is the types of bugs.
Beginner programmers may input incorrect commands, set code in an incorrect way that causing syntax errors, or instruct a computer to do something impossible.
Professional programmers know which commands are useful, how to compile their code in a proper way, and what a computer can do. However, they have their blind spots, such as failing to consider the full definition of their code or tending to compile their code in a more complicated way, which makes the bugs more difficult to find.
Bugs don’t come from nowhere
Computers take your commands seriously and hardware errors seldom occur. If your program is not executed as expected, it is usually your mistakes because computers just run as instructed.
Therefore, bugs are most likely caused by the mistakes you have made in your code.
Generally, we find out bugs by reading our code carefully and resolve the bugs by modifying the code.
Rubber duck debugging
You may find this quite common to you:
One of your friends comes to ask you a question but leaves after the question is stated or sometimes even when the question is half stated.
It is said that the best programmers usually take a rubber duck with them and put it on the desk when they debug code. They explain each line of code to the rubber duck in detail and may find out their problems during the explanation.
Likewise, you can try to raise questions and explain your code to someone else, someone that may not know Python at all. You may find out the bugs and find the solution during the explanation. Explaining the intention of your code while observing the outcome of the code allows you to easily see the gap between the two and thus find out your mistakes.
You can also explain your code to any other things if you don’t have a rubber duck around, such as a flower or plant on the desk, or even the keyboard or mouse.
Ask for help
What if a bug still exists after you spend more than one hour in checking and modifying your code?
What you need to do is ask for help.
Don’t hesitate to ask questions. The best programmers always ask questions. They know how to raise questions and what are the best questions. Asking questions doesn’t mean that you don’t know what you are doing, instead, it means that you are exploring your code and you are passionate about your project.
Therefore, you need to learn to ask questions in a proper way.
When bugs occur in your program, you can search for the error information in search engines, such as Baidu or Google. Others may have encountered the problems similar to yours, and their solutions can be used as reference.
In one word, ask your classmates or teachers for help after you have tried but still can’t solve a problem.