Page 50 - Python Basics: A Practical Introduction to Python 3
P. 50
3.2. Mess Things Up
print(Hello, World)
Did you notice how the text color changed to black when you removed
the quotation marks? IDLE no longer recognizes Hello, World as text.
What do you think will happen when you run the program? Press F5
to find out!
The following text displays in red in the interactive window:
Traceback (most recent call last):
File "/home/hello_world.py", line 1, in <module>
print(Hello, World)
NameError: name 'Hello' is not defined
Whenever an error occurs, Python stops executing the program and
displays several lines of text called a traceback. The traceback shows
useful information about the error.
Tracebacks are best read from the bottom up:
• The last line of the traceback tells you the name of the error and
the error message. In this case, a NameError occurred because the
name Hello is not defined anywhere.
• The second to last line shows you the code that produced the error.
There’s only one line of code in hello_world.py, so it’s not hard to
guess where the problem is. This information is more helpful for
larger files.
• The third to last line tells you the name of the file and the line num-
ber so you can go to the exact spot in your code where the error
occurred.
In the next section, you’ll see how to define names for values in your
code. Before you move on, though, you can get some practice with
syntax errors and runtime errors by working on the review exercises.
49