Skip to content

Commit 4c2a1b4

Browse files
committed
added listing tag to blocks
1 parent f34c039 commit 4c2a1b4

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,21 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
264264
In Python, if you want a program to continue running when an error has occurred, you can use <c>try-except</c> blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so:
265265
</p>
266266

267-
<program xml:id="python-square-input" interactive="activecode" language="python">
267+
<listing xml:id="python-square-input">
268+
<program interactive="activecode" language="python">
268269
<code>
269270
number = int(input("Please enter a whole number: "))
270271
squared = number ** 2
271272
print("Your number squared is " + str(squared))
272273
</code>
273274
</program>
275+
</listing>
274276

275277
<p>
276278
The Java code that would perform the same task is a little more complex and utilizes the <c>Scanner</c> class for input.
277279
</p>
278-
279-
<program xml:id="java-square-input" interactive="activecode" language="java">
280+
<listing xml:id="java-square-input">
281+
<program interactive="activecode" language="java">
280282
<code>
281283
import java.util.Scanner;
282284

@@ -293,12 +295,13 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
293295
}
294296
</code>
295297
</program>
298+
</listing>
296299

297300
<p>
298301
This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add <c>try-except</c> (Python) or <c>try-catch</c> (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. Adding <c>try-except</c> blocks and a <c>while</c> loop to the Python code will look something like this:
299302
</p>
300-
301-
<program xml:id="python-square-input-exception" interactive="activecode" language="python">
303+
<listing xml:id="python-square-input-exception">
304+
<program interactive="activecode" language="python">
302305
<code>
303306
while True:
304307
try:
@@ -310,12 +313,13 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
310313
print("That was not a valid number. Please try again: ")
311314
</code>
312315
</program>
316+
</listing>
313317

314318
<p>
315319
Now that we have Python code that will continuously prompt the user until they enter a whole number, let's look at Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code.
316320
</p>
317-
318-
<program xml:id="java-square-input-exception" interactive="activecode" language="java">
321+
<listing xml:id="java-square-input-exception">
322+
<program interactive="activecode" language="java">
319323
<code>
320324
import java.util.Scanner;
321325
import java.util.InputMismatchException;
@@ -340,6 +344,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
340344
}
341345
</code> <stdin> </stdin> <tests> </tests>
342346
</program>
347+
</listing>
343348

344349
<p>
345350
Firstly, let's talk about the extra import alongside the <c>Scanner</c> import. In Java, we need to import <c>InputMismatchException</c> because it's not automatically available like basic exceptions. This is different from Python where most exceptions are readily accessible. If you ran the previous Java codeblock without <c>try-catch</c> blocks and entered an erroneous input, you would have got an <c>InputMismatchException</c> exception despite not having imported this class. That being said, removing the explicit import of this library for the <c>try-catch</c> code block above will lead to compilation errors.

0 commit comments

Comments
 (0)