If the output from your "Hello, World!" program isn't what you expect and/or if you get an error message in the Output Window, this is likely due to making a mistake typing in your code. Don’t worry, this happens to all of us at some point or another – you may have made a little mistake (like misspelling a word) or you may have made a bigger mistake. Luckily, with this first program, we should be able to figure out pretty easily where the mistake is.
And even if you didn't make any mistakes, follow along, as this information will be important when the code gets more complex and errors are inevitable...
Let’s discuss where the common mistakes might be in your program, and once you think you have them fixed, go ahead and click the Play icon again to have the computer run the program another time.
Common Mistake #1: Capitalization is Important
The most common mistake is not capitalize words in your program correctly. 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. I don't expect that we'll need any fancy tools for the code we'll be writing, but it will be helpful to learn a little bit about the error messages that the IDE 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 I asked you to frint something, you wouldn't know what I was 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 your Code 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/Untitled1.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 can be ignored. 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, the location line of
code you're going to need to fix is going to be indicated in the two
lines right before the final line of the error message.