Debugging Python Code
How This (does not) WorkThis program does not work! Python says there is a syntax error which means that the script does not satisfy the structure that Python expects to see. When we observe the error given by Python, it also tells us the place where it detected the error as well. So we start debugging our program from that line.
Two Command Environments
Debugging often begins with a small experiment: enter one statement, observe what Python reports, and decide what to inspect next. Before you can use that feedback cycle, you must know which environment is receiving your command. An operating system terminal and the Python interpreter are separate environments with different prompts and different responsibilities.
The >>> prompt belongs to Python. The $ or C:\> prompt belongs to the operating system. The prompt is therefore a quick visual check: it tells you which environment will interpret what you type next.
Entering Python
The transition into Python begins at the operating system terminal. First, open the terminal so its operating system prompt is ready to receive a command. Then type python and press Enter. The operating system launches the Python interpreter, and the prompt changes to >>>. That change signals that Python code, rather than an operating system command, can now be entered.
- Open the operating system terminal.
- At the operating system prompt, type python.
- Press Enter.
- Confirm that the prompt changes to >>>.
- Enter Python statements only after the >>> prompt appears.
The REPL Feedback Cycle
The Python interpreter uses a cycle called the REPL: Read, Evaluate, Print, Loop. You type a statement at >>> and press Enter. Python reads the statement, evaluates or executes it, prints the result, and then shows >>> again. The returning prompt means the interpreter is ready for another statement.
One Statement Through the REPL
What should you observe when you enter the source example at the Python prompt?
Enter the statement: At the >>> prompt, enter print "Hello World" and press Enter.
Observe the output: Python executes the statement and displays Hello World on the next line.
Watch for the prompt: The >>> prompt appears again, showing that the interpreter is ready for another statement.
The interpreter gives immediate feedback: the statement is processed, its output appears, and the prompt returns.
Using Feedback to Debug
The REPL is useful for debugging because it shortens the distance between an attempted statement and the feedback about it. You can test small pieces of code, see the result immediately, and adjust your thinking without first writing and running a complete program.
When Python reports a syntax error, it is telling you that the script does not satisfy the structure Python expects. The error report also identifies the place where Python detected the problem. Begin debugging from that reported line. The reported location is the starting point for inspection, because it is where Python noticed that the structure was not valid.
When a syntax error appears, start at the line Python reports. Inspect that location and use the error message as the first clue about what part of the expected structure was not satisfied.
Mistakes at the Prompts
Typing Python code at the operating system prompt
The operating system terminal understands operating system commands such as cd, ls, or dir; it does not understand Python code.
Fix:
Type python first, press Enter, and wait for the >>> prompt before entering Python statements.Treating >>> as an operating system prompt
The >>> prompt signals that you are inside the Python interpreter, where Python code is the native language.
Fix:
Use Python statements at >>>. Exit Python with exit() when you need to return to the operating system terminal.Ignoring the line named by a syntax error
Python reports the place where it detected the structural problem, giving you a starting point for debugging.
Fix:
Begin by inspecting the reported line and the structure Python expected there.Expecting the Python prompt to disappear permanently after one statement
The returning prompt is the Loop part of the REPL cycle. It means Python is ready for another statement.
Fix:
Treat the returning >>> as immediate feedback that the interpreter is ready for the next experiment.
Leaving the Interpreter
When you finish experimenting, type exit() at the >>> prompt and press Enter. Python closes, and the operating system terminal prompt appears again. This return to $ or C:\> confirms that you have left the Python interpreter and can use operating system commands or launch Python again.
Prompt Recognition Practice
You open a terminal and see a $ prompt. You want to test a Python statement immediately. What should you do, and what prompt should appear before you enter the statement?
Hints
- The operating system prompt is not the Python interpreter.
- Use the command that launches Python, then look for the three greater-than symbols.
A Python script produces a syntax error and Python identifies a line. What should be your first debugging action?
Hints
- The reported location is where Python detected the problem.
- Start your inspection there and consider whether the script has the structure Python expects.
Debugging Takeaways
- The operating system prompt is shown as $ or C:\>; the Python interpreter prompt is >>>.
- Type python at the operating system terminal and press Enter to open the Python interpreter.
- At >>>, Python reads, evaluates, prints, and loops back to the prompt after each statement.
- The returning >>> prompt means Python is ready for another statement.
- For a syntax error, begin debugging at the location Python reports as the place where it detected the problem.
- Type exit() at >>> to return to the operating system terminal.
Key Takeaways
- The operating system terminal and the Python interpreter are separate command environments.
- The >>> prompt confirms that Python is ready to receive a Python statement.
- The REPL provides immediate feedback through the Read, Evaluate, Print, and Loop cycle.
- A syntax-error location is the place to begin inspecting a program's structure.
- Use exit() to leave Python and return to the operating system terminal.