If the output from one of your programs isn't what you expect or if you get an error message in the Output Window, this is likely due to a mistake in your code.
Let's use our HELLO, WORLD! 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.
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 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:
Line 1: This line is generally always the same. It is just indicating that the lines following it will be providing information on the error.
Line 2: This tells you where the error occured -- both the name of the file and the line number. Note that your code could be written in multiple files, so it's possible that the code you see in your Code Window isn't the code that has the specific issue you're being alerted to.
Line 3: This tells you the specific line of code that the computer had an issue with. It should be the line that is mentioned in Line 2, but this way you can easily see the bad line of code.
Line 4: This is the error (the specific problem that the computer had with your code). In some cases, the error will be clear enough that you'll know exactly what the problem is; in other cases, you'll have to do a little more investigation, as the error isn't as simple as the computer not recognizing a command.
Line 5: This just indicates that the program has completed or was stopped due to the error.
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
occured.