Skip to main content

Python: 4 My program doesn't work (Debugging)

When writing programs you will often encounter errors.

Part of the programming process is testing your code and ensuring that it works as intended.

The more you code the easier it will be to detect these types of errors.

Syntax errors are the most common form of error.

These are when the compiler (or in Python interpreter) cannot understand the code as there are parsing (typed/reading) errors in it.

This could be misspelled keywords, colon, incorrect indentation, unpaired bracket or quote marks.

Other errors can be exceptions where the code is written correctly but when the program runs it encounters a problem.

This could be dividing by 0, trying to convert text to a number, using a variable that hasn't been instantiated (initialised/used)

Note that you will get a message telling you where the error has been found. It might not be here exactly but it will give you a good starting point.

It could also be a logical error where you are checking the condition in an if statement or loop incorrectly or not handling it correctly

Take the program below. It should loop five times but gets stuck in an infinite loop as the count variable is not added to and will always remain 0

Or by not being specific enough you might not get the expected output