Debugging Your Python Code

It goes without saying that you’ll eventually come across an error in your code, where Python declares it’s not able to continue due to something being missed out, wrong or simply unknown. Being able to identify these errors makes for a good programmer.

Debugging

Errors in code are called bugs and are perfectly normal. They can often be easily rectified with a little patience. The important thing is to keep looking, experimenting and testing. Eventually your code will be bug free.

Purely out of interest, the word debugging in computing terms comes from Admiral Grace Hopper,
who back in the ‘40s was working on a monolithic Harvard Mark II electromechanical computer. According to legend Hopper found a moth stuck in a relay, thus stopping the system from working. Removal of the moth was hence called debugging.

Code isn’t as fluid as the written word, no matter how good the programming language is. Python is certainly easier than most languages but even it is prone to some annoying bugs. The most common are typos by the user and whilst easy to find in simple dozen-line code, imagine having to debug multi-thousand line code.

The most common of errors is the typo, as we’ve mentioned. The typos are often at the command level: mistyping the print command for example. However, they also occur when you have numerous variables, all of which have lengthy names. The best advice is to simply go through the code and check your spelling.

Thankfully Python is helpful when it comes to displaying error messages. When you receive an error, in red text from the IDLE Shell, it will define the error itself along with the line number where the error has occurred. Whilst in the IDLE Editor this is a little daunting for lots of code; text editors help by including line numbering.

Syntax errors are probably the second most common errors you’ll come across as a programmer. Even if the spelling is correct, the actual command itself is wrong. In Python 3 this often occurs when Python 2 syntaxes are applied. The most annoying of these is the print function. In Python 3 we use print(“words”), whereas Python2 uses print “words”.

Pesky brackets are also a nuisance in programming errors, especially when you have something like:

print(balanced_check(input()))

Remember that for every ‘(‘ there must be an equal number of ‘)’.

There are thousands of online Python resources, code snippets and lengthy discussions across forums on how best to achieve something. Whilst 99 per cent of it is good code, don’t always be lured into copying and pasting random code into your editor. More often than not, it won’t work and the worst part is that you haven’t learnt anything.

Indents are a nasty part of Python programming that a lot of beginners fall foul of. Recall the If loop from the Conditions and Loops section, where the colon means everything indented following the statement is to be executed as long as it’s true? Missing the indent, or having too much of indent, will come back with an error.

An excellent way to check your code step-by-step is to use Python Tutor’s Visualise web page, found at www.pythontutor.com/visualize.html#mode=edit. Simply paste your code into the editor and click the Visualise Execution button to run the code line-by-line. This helps to clear bugs and any misunderstandings.

Planning makes for good code. Whilst a little old school, it’s a good habit to plan what your code will do before sitting down to type it out. List the variables that will be used and the modules too; then write out a script for any user interaction or outputs.

Russ Ware

Russ has been testing, reviewing and writing guides for tech since the heady days of Windows 95 and the Sega Saturn. A self-confessed (and proud) geek about all things tech, if it has LED's, a screen, beeps or has source code, Russ will want to master it (and very likely take it apart to see how it works...)

Related Articles

Back to top button