Skip to content

Commit b192c3c

Browse files
committed
New: Create a pdf from PreTeXt source!!
1 parent c6bf0ba commit b192c3c

50 files changed

Lines changed: 380 additions & 319 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pretext/ClassesBasics/toctree.ptx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en-US">
3-
4<title>Classes and Objects - the Basics</title>
4-
4<xi:include href="./Objectorientedprogramming.ptx"/>
5-
4<xi:include href="./Achangeofperspective.ptx"/>
6-
4<xi:include href="./ObjectsRevisited.ptx"/>
7-
4<xi:include href="./UserDefinedClasses.ptx"/>
8-
4<xi:include href="./ImprovingourConstructor.ptx"/>
9-
4<xi:include href="./AddingOtherMethodstoourClass.ptx"/>
10-
4<xi:include href="./ObjectsasArgumentsandParameters.ptx"/>
11-
4<xi:include href="./ConvertinganObjecttoaString.ptx"/>
12-
4<xi:include href="./InstancesasReturnValues.ptx"/>
13-
4<xi:include href="./Glossary.ptx"/>
14-
4<xi:include href="./Exercises.ptx"/>
3+
<title>Classes and Objects - the Basics</title>
4+
<xi:include href="./Objectorientedprogramming.ptx"/>
5+
<xi:include href="./Achangeofperspective.ptx"/>
6+
<xi:include href="./ObjectsRevisited.ptx"/>
7+
<xi:include href="./UserDefinedClasses.ptx"/>
8+
<xi:include href="./ImprovingourConstructor.ptx"/>
9+
<xi:include href="./AddingOtherMethodstoourClass.ptx"/>
10+
<xi:include href="./ObjectsasArgumentsandParameters.ptx"/>
11+
<xi:include href="./ConvertinganObjecttoaString.ptx"/>
12+
<xi:include href="./InstancesasReturnValues.ptx"/>
13+
<xi:include href="./Glossary.ptx"/>
14+
<xi:include href="./Exercises.ptx"/>
1515
</chapter>
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en-US">
3-
4<title>Classes and Objects - Digging a Little Deeper</title>
4-
4<xi:include href="./Fractions.ptx"/>
5-
4<xi:include href="./ObjectsareMutable.ptx"/>
6-
4<xi:include href="./Sameness.ptx"/>
7-
4<xi:include href="./ArithmeticMethods.ptx"/>
8-
4<xi:include href="./Glossary.ptx"/>
9-
4<xi:include href="./Exercises.ptx"/>
3+
<title>Classes and Objects - Digging a Little Deeper</title>
4+
<xi:include href="./Fractions.ptx"/>
5+
<xi:include href="./ObjectsareMutable.ptx"/>
6+
<xi:include href="./Sameness.ptx"/>
7+
<xi:include href="./ArithmeticMethods.ptx"/>
8+
<xi:include href="./Glossary.ptx"/>
9+
<xi:include href="./Exercises.ptx"/>
1010
</chapter>

pretext/Debugging/KnowyourerrorMessages.ptx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0"?>
22
<section xml:id="debugging_know-your-error-messages">
33
<title>Know Your Error Messages</title>
4-
<introduction>
4+
<subsection>
5+
<title>Introduction</title>
56
<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>
67
<program language="python">
78
<input>
@@ -228,7 +229,7 @@ print(final_time_int)
228229
</li>
229230
</ul>
230231
</p>
231-
</introduction>
232+
</subsection>
232233
<subsection xml:id="debugging_parseerror">
233234
<title>ParseError</title>
234235
<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>
@@ -247,7 +248,7 @@ wait_time_int = int(wait_time_str)
247248
final_time_int = current_time_int + wait_time_int
248249
print(final_time_int)
249250
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+
251252
<program language="python">
252253
<input>
253254
current_time_str = input("What is the current time (in hours 0-23)?")
@@ -278,7 +279,7 @@ wait_time_int = int(wait_time_str)
278279
final_time_int = current_time_int + wait_time_int
279280
print(final_time_int)
280281
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+
282283
<program language="python">
283284
<input>
284285
current_time_str = input("What is the "current time" (in hours 0-23)?")
@@ -296,7 +297,7 @@ print(final_time_int)
296297
</solution>
297298
</exercise>
298299
<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>
300301
<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>
301302
</subsection>
302303
<subsection xml:id="debugging_typeerror">
@@ -449,6 +450,7 @@ print(final_time_int)
449450
</input>
450451
</program>
451452
<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>
452454
<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>
453455
</subsection>
454456
</section>

pretext/Debugging/toctree.ptx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en-US">
3-
4<title>Debugging Interlude 1</title>
4-
4<xi:include href="./intro-HowtobeaSuccessfulProgrammer.ptx"/>
5-
4<xi:include href="./HowtoAvoidDebugging.ptx"/>
6-
4<xi:include href="./BeginningtipsforDebugging.ptx"/>
7-
4<xi:include href="./KnowyourerrorMessages.ptx"/>
8-
4<xi:include href="./Summary.ptx"/>
9-
4<xi:include href="./Exercises.ptx"/>
3+
<title>Debugging Interlude 1</title>
4+
<xi:include href="./intro-HowtobeaSuccessfulProgrammer.ptx"/>
5+
<xi:include href="./HowtoAvoidDebugging.ptx"/>
6+
<xi:include href="./BeginningtipsforDebugging.ptx"/>
7+
<xi:include href="./KnowyourerrorMessages.ptx"/>
8+
<xi:include href="./Summary.ptx"/>
9+
<xi:include href="./Exercises.ptx"/>
1010
</chapter>

pretext/Dictionaries/toctree.ptx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en-US">
3-
4<title>Dictionaries</title>
4-
4<xi:include href="./intro-Dictionaries.ptx"/>
5-
4<xi:include href="./Dictionaryoperations.ptx"/>
6-
4<xi:include href="./Dictionarymethods.ptx"/>
7-
4<xi:include href="./Aliasingandcopying.ptx"/>
8-
4<xi:include href="./Sparsematrices.ptx"/>
9-
4<xi:include href="./Glossary.ptx"/>
10-
4<xi:include href="./Exercises.ptx"/>
3+
<title>Dictionaries</title>
4+
<xi:include href="./intro-Dictionaries.ptx"/>
5+
<xi:include href="./Dictionaryoperations.ptx"/>
6+
<xi:include href="./Dictionarymethods.ptx"/>
7+
<xi:include href="./Aliasingandcopying.ptx"/>
8+
<xi:include href="./Sparsematrices.ptx"/>
9+
<xi:include href="./Glossary.ptx"/>
10+
<xi:include href="./Exercises.ptx"/>
1111
</chapter>

pretext/Exceptions/05_exceptions_syntax.ptx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0"?>
22
<section xml:id="exceptions_exceptions-syntax">
33
<title>Exceptions Syntax</title>
4-
<introduction>
4+
<subsection><title>Introduction</title>
55
<p>There are many variations on the code that catches exceptions. Here is a
66
brief summary, but other code variations are possible.</p>
7-
</introduction>
7+
</subsection>
88
<subsection xml:id="exceptions_catch-all-exceptions">
99
<title>Catch All Exceptions</title>
1010
<p>Catch all exceptions, regardless of their type. This will prevent

pretext/Exceptions/toctree.ptx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en-US">
3-
4<title>Exceptions</title>
4-
4<xi:include href="./01_intro_exceptions.ptx"/>
5-
4<xi:include href="./02_runtime_stack_and_raise_command.ptx"/>
6-
4<xi:include href="./03_standard_exceptions.ptx"/>
7-
4<xi:include href="./04_principles_for_using_exceptions.ptx"/>
8-
4<xi:include href="./05_exceptions_syntax.ptx"/>
9-
4<xi:include href="./06_the_finally_clause.ptx"/>
10-
4<xi:include href="./glossary.ptx"/>
11-
4<xi:include href="./Exercises.ptx"/>
3+
<title>Exceptions</title>
4+
<xi:include href="./01_intro_exceptions.ptx"/>
5+
<xi:include href="./02_runtime_stack_and_raise_command.ptx"/>
6+
<xi:include href="./03_standard_exceptions.ptx"/>
7+
<xi:include href="./04_principles_for_using_exceptions.ptx"/>
8+
<xi:include href="./05_exceptions_syntax.ptx"/>
9+
<xi:include href="./06_the_finally_clause.ptx"/>
10+
<xi:include href="./glossary.ptx"/>
11+
<xi:include href="./Exercises.ptx"/>
1212
</chapter>

pretext/Figures/turtleTest1.png

10.4 KB
Loading

pretext/Files/toctree.ptx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en-US">
3-
4<title>Files</title>
4-
4<xi:include href="./intro-WorkingwithDataFiles.ptx"/>
5-
4<xi:include href="./FindingaFileonyourDisk.ptx"/>
6-
4<xi:include href="./ReadingaFile.ptx"/>
7-
4<xi:include href="./Iteratingoverlinesinafile.ptx"/>
8-
4<xi:include href="./AlternativeFileReadingMethods.ptx"/>
9-
4<xi:include href="./WritingTextFiles.ptx"/>
10-
4<xi:include href="./WithStatements.ptx"/>
11-
4<xi:include href="./FetchingSomethingFromTheWeb.ptx"/>
12-
4<xi:include href="./Glossary.ptx"/>
13-
4<xi:include href="./Exercises.ptx"/>
3+
<title>Files</title>
4+
<xi:include href="./intro-WorkingwithDataFiles.ptx"/>
5+
<xi:include href="./FindingaFileonyourDisk.ptx"/>
6+
<xi:include href="./ReadingaFile.ptx"/>
7+
<xi:include href="./Iteratingoverlinesinafile.ptx"/>
8+
<xi:include href="./AlternativeFileReadingMethods.ptx"/>
9+
<xi:include href="./WritingTextFiles.ptx"/>
10+
<xi:include href="./WithStatements.ptx"/>
11+
<xi:include href="./FetchingSomethingFromTheWeb.ptx"/>
12+
<xi:include href="./Glossary.ptx"/>
13+
<xi:include href="./Exercises.ptx"/>
1414
</chapter>

pretext/Functions/TheAccumulatorPattern.ptx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<section xml:id="functions_the-accumulator-pattern">
33
<title>The Accumulator Pattern</title>
4-
<introduction>
4+
<subsection><title>Introduction</title>
55
<video xml:id="function_accumulator_pattern" youtube="aqhREpceEMI" width="auto"/>
66
<p>In the previous example, we wrote a function that computes the square of a number. The algorithm we used
77
in the function was simple: multiply the number by itself.
@@ -61,7 +61,7 @@ squareResult = square(toSquare)
6161
print("The result of", toSquare, "squared is", squareResult)
6262
</input>
6363
</program>
64-
</introduction>
64+
</subsection>
6565
<subsection xml:id="functions_the-general-accumulator-pattern">
6666
<title>The General Accumulator Pattern</title>
6767
<program language="python">

0 commit comments

Comments
 (0)