Home

How to Spot and Fix Errors in Python

|
|  Updated:  
2023-08-10 16:08:09
Python Essentials For Dummies
Explore Book
Buy On Amazon

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

About This Article

This article is from the book: 

About the book author:

Brendan Scott is a dad who loves Python and wants kids to get some of its magic too. He started pythonforkids.brendanscott.com to help teach his oldest child to code. He maintains it to help other young people learn Python.