|
1 | 1 | <?xml version="1.0"?> |
2 | 2 | <section xml:id="debugging_know-your-error-messages"> |
3 | 3 | <title>Know Your Error Messages</title> |
| 4 | + <introduction> |
4 | 5 | <p>Many problems in your program will lead to an error message. For example as I was writing and testing this chapter of the book I wrote the following version of the example program in the previous section.</p> |
5 | 6 | <program language="python"> |
6 | 7 | <input> |
@@ -227,10 +228,11 @@ print(final_time_int) |
227 | 228 | </li> |
228 | 229 | </ul> |
229 | 230 | </p> |
| 231 | + </introduction> |
230 | 232 | <subsection xml:id="debugging_parseerror"> |
231 | 233 | <title>ParseError</title> |
232 | 234 | <p>Parse errors happen when you make an error in the syntax of your program. Syntax errors are like making grammatical errors in writing. If you don't use periods and commas in your writing then you are making it hard for other readers to figure out what you are trying to say. Similarly Python has certain grammatical rules that must be followed or else Python can't figure out what you are trying to say.</p> |
233 | | - <p>Usually ParseErrors can be traced back to missing punctuation characters, such as parentheses, quotation marks, or commas. Remember that in Python commas are used to separate parameters to functions. Paretheses must be balanced, or else Python thinks that you are trying to include everything that follows as a parameter to some function.</p> |
| 235 | + <p>Usually ParseErrors can be traced back to missing punctuation characters, such as parentheses, quotation marks, or commas. Remember that in Python commas are used to separate parameters to functions. Parentheses must be balanced, or else Python thinks that you are trying to include everything that follows as a parameter to some function.</p> |
234 | 236 | <p>Here are a couple examples of Parse errors in the example program we have been using. See if you can figure out what caused them.</p> |
235 | 237 | <exercise> |
236 | 238 | <statement/> |
@@ -349,7 +351,7 @@ print(time_when_alarm_go_off) |
349 | 351 | </hint> |
350 | 352 | <p><term>Finding Clues</term> With name errors one of the best things you can do is use the editor, or browser search function. Quite often if you search for the exact word in the error message one of two things will happen:</p> |
351 | 353 | <p> |
352 | | - <ol label="1"> |
| 354 | + <ol marker="1"> |
353 | 355 | <li> |
354 | 356 | <p>The word you are searching for will appear only once in your code, it's also likely that it will be on the right hand side of an assignment statement, or as a parameter to a function. That should confirm for you that you have a typo somewhere. If the name in question <term>is</term> what you thought it should be then you probably have a typo on the left hand side of an assignment statement on a line before your error message occurs. Start looking backward at your assignment statements. In some cases it's really nice to leave all the highlighted strings from the search function visible as they will help you very quickly find a line where you might have expected your variable to be highlighted.</p> |
355 | 357 | </li> |
@@ -390,7 +392,7 @@ print(alarm_time) |
390 | 392 | <title>Solution</title> |
391 | 393 | <p>In this example the error message is about <c>set_time</c> not defined on line 3. In this case the undefined name is not used in an assignment statement, but is used as a parameter (incorrectly) to a function call. A search on <c>set_time</c> reveals that in fact it is only used once in the program. Did the author mean <c>set_alarm</c>? If we make that assumption we immediately get another error <c>NameError: name 'alarm_time' is not defined on line: 3</c>. The variable <c>alarm_time</c> is defined on line 4, but that does not help us on line 3. Furthermore we now have to ask the question is this function call <c>int(present_time, set_alarm, alarm_time)</c> even the correct use of the <c>int</c> function? The answer to that is a resounding no. Let's list all of the things wrong with line 3:</p> |
392 | 394 | <p> |
393 | | - <ol label="1"> |
| 395 | + <ol marker="1"> |
394 | 396 | <li> |
395 | 397 | <p><c>set_time</c> is not defined and never used, the author probably meant <c>set_alarm</c>.</p> |
396 | 398 | </li> |
|
0 commit comments