HANDLING ERRORS

If the output from one of your programs isn't what you expect or if you get an error message in the Program Output window, this is likely due to a mistake in your code.

Common Programming Mistakes

Let's use our project as an example. Recall, the program from that example was:

If you've received error when running this code, it's likely due to one of the common mistakes below.

Common Mistake #1: Capitalization is Important

The most common mistake is incorrect capitalization of words in your program. In this case, the word print is a command word (it tells the computer to do something), and it needs to be all lowercase. Verify that you haven't capitalized any of the letters in the word print.

Common Mistake #2: Not Using Parentheses – "(" and ")" – Correctly

If you've checked your capitalization and that wasn't the problem, next verify that you have parentheses around the "Hello, World!" part of your code.

Common Mistake #3: Not Using Quotation Marks – (") – Correctly

If your capitalization is correct and you've used the parentheses correctly, the next thing to check is that you've correctly put quotation marks around "Hello, World!" It doesn't matter if you've chosen to use single quotation marks (') or double quotation marks ("), as long as you have the same on both sides.

Interpreting Error Messages

Hopefully, by now, you've been able to fix any mistakes and the program is running as you expect. But, there will be times — especially as your code gets more complicated — that fixing mistakes isn't as easy as those above. In fact, for programmers who are writing very complex code, there are lots of tools and other software strictly aimed at "debugging" (fixing) code. Let's learn a little bit about the error messages that the RDE will spit out when you make a mistake.

As an example, let's change your original code above to the following:

You'll notice that we "accidentally" changed the word "print" to the word "frint." Because Python doesn't have a command called "frint," it doesn't know what to do what this instruction (just like if we asked you to frint something, you wouldn't know what we were talking about either). So, instead of trying to guess what you wanted to do, the computer instead stops running your code and gives you an error message, which is displayed in the Program Output window.

Here is the error message you will see (or something very close to this):

Traceback (most recent call last):
   File "/home/pi/projects/Untitled.py", line 1 in "module"
      frint("Hello, World!")
NameError: name 'frint' is not defined
-- PROGRAM FINISHED --

There are four lines to this error message — let's look at each individually:

The simplest error messages will have four lines, just like this one. Line 1 will always be the same as above and Line 4 will always contain the specific error that the computer is complaining about (Line 5 may change to -- PROGRAM STOPPED --). But, sometimes the computer will find errors that stem from multiple locations. If the errors stem from multiple locations, Lines 2 and 3 may be repeated multiple times, each time with a different file/line number (on the first line) and line of code (on the second line). In general, look towards the end of the error message for the location and type of error that occurred.