Skip to content

Commit 20554f2

Browse files
committed
more improvements of iteration chapter
1 parent 884b600 commit 20554f2

File tree

5 files changed

+177
-95
lines changed

5 files changed

+177
-95
lines changed

bookbase

text/main/controlFlow/exceptions/exceptions.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@
220220
This process termination can be observed in program \programUrl{exceptions:use_sqrt_raise} shown as \cref{lst:exceptions:use_sqrt_raise}.
221221
In the program, we iteratively apply our new \pythonil{sqrt} function to the values inside a \pythonil{tuple} using a \pythonilIdx{for}~loop.
222222
We write the results of these computations to the \glsFull{stdout} using \pgls{fstring} and \pythonil{print}.
223-
We here use the optional parameter \pythonilIdx{flush} of \pythonilIdx{print}, which has the default value~\pythonil{False}.
224-
We set it to \pythonil{flush=True}, though, which forces that the output of \pythonilIdx{print} is directly and immediately written to the \pgls{stdout} and not cached.
223+
We here use the optional parameter \pythonilIdx{flush}\pythonIdx{print!flush} of \pythonilIdx{print}, which has the default value~\pythonil{False}.
224+
We set it to \pythonil{flush=True}\pythonIdx{print!flush}, though, which forces that the output of \pythonilIdx{print} is directly and immediately written to the \pgls{stdout} and not cached.
225225
This is useful in this special example to prevent mix-ups between the \pgls{stdout} stream receiving the normal output of the program and the \glsFull{stderr} stream where error messages appear.
226226
Both streams are captured and presented in \cref{exec:exceptions:use_sqrt_raise} and the order of the text could otherwise get mixed up.
227227

@@ -976,7 +976,7 @@
976976
Of course, we still have to test that our function behaves as expected for valid arguments.
977977

978978
In the second test function, \pythonil{test_sqrt_raises_arithmetic_error}, we check arguments that are floating point numbers but have illegal values.
979-
In a \pythonil{for}-loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[-1.0, inf, -inf, nan]}.
979+
In a \pythonil{for}~loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[-1.0, inf, -inf, nan]}.
980980
The first value is negative and the others are not finite.
981981
The sanity check \pythonil{(not isfinite(number)) or (number < 0.0)} inside our function should therefore catch these values, unless we made an error and it somehow does not do what we thing it should be doing.
982982
Thus, all of these values should cause our \pythonil{sqrt} function to raise an \pythonilIdx{ArithmeticError}.
@@ -997,7 +997,7 @@
997997
\begin{sloppypar}%
998998
Finally, in our test function \pythonil{test_sqrt_raises_type_error}, we check for \pythonilsIdx{TypeError} being raised.
999999
We use exactly the same pattern as in the previous test function.
1000-
In a \pythonil{for}-loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[True, "x", None]}.
1000+
In a \pythonil{for}~loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[True, "x", None]}.
10011001
Obviously, none of these values are of type \pythonil{float}.
10021002
The check \pythonil{isinstance(number, float)} should prevent these values from entering the actual computation.
10031003
Unless, of course, we misunderstood how \pythonilIdx{isinstance} works.

text/main/controlFlow/functions/functions.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
%
145145
We can now compute the factorial of any positive integer number~\pythonil{x} by calling~\pythonil{factorial(x)}.
146146
After the function body, we leave two empty lines.
147-
And then we compute the factorials of the numbers from~$1$ to~$9$ in a \pythonilIdx{for}-loop and print them by using \pglspl{fstring}.
147+
And then we compute the factorials of the numbers from~$1$ to~$9$ in a \pythonilIdx{for}~loop and print them by using \pglspl{fstring}.
148148
Inside this loop and in the \pgls{fstring}, we can use the function \pythonil{factorial} exactly like any other function we used before, like \pythonil{sqrt} or \pythonil{sin}.
149149
It may be an interesting side information at the end of this example that the factorial can actually be computed \emph{faster} than using this product form, see, e.g.~\cite{L2015ANKOFF}.
150150

0 commit comments

Comments
 (0)