-->

How To Fix Python Errors

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.

You May Also Like:



Step 1

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.

How To Fix Python Errors
How To Fix Python Errors


Step 2

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.

How To Fix Python Errors



Step 3 

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.

How To Fix Python Errors



Step 4

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”.

How To Fix Python Errors



Step 5

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 ‘)’.

How To Fix Python Errors



Step 6

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.

How To Fix Python Errors


Step 7

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.

How To Fix Python Errors


Step 8

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.

How To Fix Python Errors


Step 9

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.

How To Fix Python Errors flowchart
How To Fix Python Errors flowchart


Step 10

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.


0 Response to "How To Fix Python Errors"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel