You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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>
6
7
<programlanguage="python">
7
8
<input>
@@ -228,7 +229,7 @@ print(final_time_int)
228
229
</li>
229
230
</ul>
230
231
</p>
231
-
</introduction>
232
+
</subsection>
232
233
<subsectionxml:id="debugging_parseerror">
233
234
<title>ParseError</title>
234
235
<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>
Since the error message points us to line 4 this might be a bit confusing. If you look at line four carefully you will see that there is no problem with the syntax. So, in this case the next step should be to back up and look at the previous line. In this case if you look at line 2 carefully you will see that there is a missing right parenthesis at the end of the line. Remember that parenthses must be balanced. Since Python allows statements to continue over multiple lines inside parentheses Python will continue to scan subsequent lines looking for the balancing right parenthesis. However in this case it finds the name current_time_int and it will want to interpret that as another parameter to the input function. But, there is not a comma to separate the previous string from the variable so as far as Python is concerned the error here is a missing comma. From your perspective its a missing parenthesis.
250
-
251
+
251
252
<programlanguage="python">
252
253
<input>
253
254
current_time_str = input("What is the current time (in hours 0-23)?")
The error message points you to line 1 and in this case that is exactly where the error occurs. In this case your biggest clue is to notice the difference in highlighting on the line. Notice that the words <q>current time</q> are a different color than those around them. Why is this? Because <q>current time</q> is in double quotes inside another pair of double quotes Python thinks that you are finishing off one string, then you have some other names and finally another string. But you haven't separated these names or strings by commas, and you haven't added them together with the concatenation operator (+). So, there are several corrections you could make. First you could make the argument to input be as follows: "What is the 'current time' (in hours 0-23)" Notice that here we have correctly used single quotes inside double quotes. Another option is to simply remove the extra double quotes. Why were you quoting <q>current time</q> anyway? "What is the current time (in hours 0-23)"
281
-
282
+
282
283
<programlanguage="python">
283
284
<input>
284
285
current_time_str = input("What is the "current time" (in hours 0-23)?")
@@ -296,7 +297,7 @@ print(final_time_int)
296
297
</solution>
297
298
</exercise>
298
299
<p>
299
-
<term>Finding Clues</term> If you follow the same advice as for the last problem, comment out line one, you will immediately get a different error message. Here's where you need to be very careful and not panic. The error message you get now is: <c>NameError: name 'current_time_str' is not defined on line 4</c>. You might be very tempted to think that this is somehow related to the earlier problem and immediately conclude that there is something wrong with the variable name <c>current_time_str</c> but if you reflect for a minute you will see that by commenting out line one you have caused a new and unrelated error. That is you have commented out the creation of the name <c>current_time_str</c>. So of course when you want to convert it to an <c>int</c> you will get the NameError. Yes, this can be confusing, but it will become much easier with experience. It's also important to keep calm, and evaluate each new clue carefully so you don't waste time chasing problems that are not really there.</p>
300
+
<term>Finding Clues</term> If you follow the same advice as for the last problem, comment out line one, you will immediately get a different error message. Here's where you need to be very careful and not panic. The error message you get now is: <c>NameError: name 'current_time_str' is not defined on line </c>. You might be very tempted to think that this is somehow related to the earlier problem and immediately conclude that there is something wrong with the variable name <c>current_time_str</c> but if you reflect for a minute you will see that by commenting out line one you have caused a new and unrelated error. That is you have commented out the creation of the name <c>current_time_str</c>. So of course when you want to convert it to an <c>int</c> you will get the NameError. Yes, this can be confusing, but it will become much easier with experience. It's also important to keep calm, and evaluate each new clue carefully so you don't waste time chasing problems that are not really there.</p>
300
301
<p>Uncomment line 1 and you are back to the ParseError. Another track is to eliminate a possible source of error. Rather than commenting out the entire line you might just try to assign <c>current_time_str</c> to a constant value. For example you might make line one look like this: <c>current_time_str = "10" #input("What is the "current time" (in hours 0-23)?")</c>. Now you have assigned <c>current_time_str</c> to the string 10, and commented out the input statement. And now the program works! So you conclude that the problem must have something to do with the input function.</p>
301
302
</subsection>
302
303
<subsectionxml:id="debugging_typeerror">
@@ -449,6 +450,7 @@ print(final_time_int)
449
450
</input>
450
451
</program>
451
452
<p>Run the program but instead of typing in anything to the dialog box just click OK. You should see the following error message: <c>ValueError: invalid literal for int() with base 10: '' on line: 4</c> This error is not because you have made a mistake in your program. Although sometimes we do want to check the user input to make sure its valid, but we don't have all the tools we need for that yet. The error happens because the user did not give us something we can convert to an integer, instead we gave it an empty string. Try running the program again. Now this time enter <q>ten</q> instead of the number 10. You will get a similar error message.</p>
453
+
<p>Run the program but instead of typing in anything to the dialog box just click OK. You should see the following error message: <c>ValueError: invalid literal for int() with base 10: '' on line: </c> This error is not because you have made a mistake in your program. Although sometimes we do want to check the user input to make sure its valid, but we don't have all the tools we need for that yet. The error happens because the user did not give us something we can convert to an integer, instead we gave it an empty string. Try running the program again. Now this time enter <q>ten</q> instead of the number 10. You will get a similar error message.</p>
452
454
<p>ValueErrors are not always caused by user input error, but in this program that is the case. We'll look again at ValueErrors again when we get to more complicated programs. For now it is worth repeating that you need to keep track of the restrictions needed for your variables, and understand what your function is expecting. You can do this by writing comments in your code, or by naming your variables in a way that reminds you of their proper form.</p>
0 commit comments