Programming with Python · free preview
Errors and Exceptions
To be able to read a traceback, and determine where the error took place and what type it is. To be able to describe the types of situations in which syntax errors, indentation errors, name errors, index errors, and missing file errors occur.
1. Overview
Overview
2. Long Tracebacks
Long Tracebacks
3. Reading Error Messages
Reading Error Messages
4. Better errors on newer Pythons
Better errors on newer Pythons
5. Syntax Errors
Syntax Errors
6. Tabs and Spaces
Tabs and Spaces
7. Variable Name Errors
Variable Name Errors
8. Index Errors
Index Errors
9. File Errors
File Errors
10. Identifying Syntax Errors
Identifying Syntax Errors
11. Identifying Variable Name Errors
Identifying Variable Name Errors
12. Identifying Index Errors
Identifying Index Errors
13. Repair the print call
Fix this failing line exactly: pritn("ready"). Write the corrected line.
14. Trace a failed index access and its fix
Predict the complete failure first, what prints, what the last traceback line says, and which line the arrow points to. Then predict exactly what the fixed program prints. Run the fixed program and compare it with your trace.
Example. Trace a failed index access and its fix
Problem. Predict the complete failure first, what prints, what the last traceback line says, and which line the arrow points to. Then predict exactly what the fixed program prints. Run the fixed program and compare it with your trace.
Correct solution.
Broken program.```python def favorite_ice_cream():
ice_creams = [ 'chocolate', 'vanilla', 'strawberry' ] print(ice_creams[3])
favorite_ice_cream()
The failing output ends with.```output
IndexError: list index out of rangeFixed program.```python def favorite_ice_cream():
ice_creams = [ 'chocolate', 'vanilla', 'strawberry' ] print(ice_creams[-1])
favorite_ice_cream()
```output
strawberryTrace.
- The call frame enters
favorite_ice_cream, where the local list has length 3. - The expression
ice_creams[3]asks for the fourth item, so Python raisesIndexErrorbeforeprintcan output anything. - In the fixed version,
ice_creams[-1]resolves to the string'strawberry', and the call returnsNoneafter the print.
关键提醒。
The bottom of a traceback gives the error type and message; the arrow above it gives the exact failing operation. Here the cause is an off-by-one positive index, so the fix is either the valid zero-based index 2 or the negative index -1. The function itself still returns None because it only prints.
15. Retrieval practice
Given the failing code below, state the traceback's last line and the one-line fix that will make it open the intended file. The file data/inflammation-02.csv exists, but the code names inflamation-02.csv.
data = numpy.loadtxt('inflamation-02.csv', delimiter=',')Answer with the exception type, the exact missing word in the path, and a corrected call.
16. Mastery check
Apply error handling to this new input, values = [10, 0, 5], and the program must divide each value by (value - 5). Predict the failing input, write a try/except that prints 'undefined difference' for the zero division and keeps the other divisions, and state the printed output for all three inputs.
Show the changed code path for value = 5, the surviving outputs for the other values, and the exception class you catch.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free