How to Spot and Fix Errors in Python

By Brendan Scott

The Python interpreter takes in each line and operates on it immediately (more or less) after you press the Enter key. In Hello World! you use Python’s print feature. print takes what’s inside the parentheses and outputs it to the command line (also called the console).

Python is sensitive to both the grammar and punctuation. If you misspell something, the program won’t work. If Python is expecting special characters and you don’t put them in, then Python will fail. Some Python issues are shown here. Can you work out how you would fix them?

  >>> pritn('Hello World!')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'pritn' is not defined

Here’s another:

   >>> print('Hello World!)
     File "<stdin>", line 1
       print('Hello World!)
   SyntaxError: EOL while scanning string literal

Here’s another:

   >>> print 'Hello World!')
     File "<stdin>", line 1
       print 'Hello World!')
   SyntaxError: invalid syntax

Python tries to give you the reason it failed (that is, NameError and SyntaxError).

Check each of these things:

  • Opening parenthesis has a closing parenthesis

  • Every opening quote mark has a matching closing quote mark

  • All commands are correctly spelled