Online Book Reader

Home Category

Learning Python - Mark Lutz [165]

By Root 1772 0
code and an else part to be run if no exception is raised in the try part. Python first runs the try part, then runs either the except part (if an exception occurs) or the else part (if no exception occurs).

In terms of statement nesting, because the words try, except, and else are all indented to the same level, they are all considered part of the same single try statement. Notice that the else part is associated with the try here, not the if. As we’ve seen, else can appear in if statements in Python, but it can also appear in try statements and loops—its indentation tells you what statement it is a part of. In this case, the try statement spans from the word try through the code indented under the word else, because the else is indented to the same level as try. The if statement in this code is a one-liner and ends after the break.

Again, we’ll come back to the try statement later in this book. For now, be aware that because try can be used to intercept any error, it reduces the amount of error-checking code you have to write, and it’s a very general approach to dealing with unusual cases. If we wanted to support input of floating-point numbers instead of just integers, for example, using try would be much easier than manual error testing—we could simply run a float call and catch its exceptions, instead of trying to analyze all possible floating-point syntax.

Nesting Code Three Levels Deep

Let’s look at one last mutation of our script. Nesting can take us even further if we need it to—we could, for example, branch to one of a set of alternatives based on the relative magnitude of a valid input:

while True:

reply = input('Enter text:')

if reply == 'stop':

break

elif not reply.isdigit():

print('Bad!' * 8)

else:

num = int(reply)

if num < 20:

print('low')

else:

print(num ** 2)

print('Bye')

This version includes an if statement nested in the else clause of another if statement, which is in turn nested in the while loop. When code is conditional, or repeated like this, we simply indent it further to the right. The net effect is like that of the prior versions, but we’ll now print “low” for numbers less than 20:

Enter text:19

low

Enter text:20

400

Enter text:spam

Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!

Enter text:stop

Bye

Chapter Summary

That concludes our quick look at Python statement syntax. This chapter introduced the general rules for coding statements and blocks of code. As you’ve learned, in Python we normally code one statement per line and indent all the statements in a nested block the same amount (indentation is part of Python’s syntax). However, we also looked at a few exceptions to these rules, including continuation lines and single-line tests and loops. Finally, we put these ideas to work in an interactive script that demonstrated a handful of statements and showed statement syntax in action.

In the next chapter, we’ll start to dig deeper by going over each of Python’s basic procedural statements in depth. As you’ll see, though, all statements follow the same general rules introduced here.

Test Your Knowledge: Quiz

What three things are required in a C-like language but omitted in Python?

How is a statement normally terminated in Python?

How are the statements in a nested block of code normally associated in Python?

How can you make a single statement span multiple lines?

How can you code a compound statement on a single line?

Is there any valid reason to type a semicolon at the end of a statement in Python?

What is a try statement for?

What is the most common coding mistake among Python beginners?

Test Your Knowledge: Answers

C-like languages require parentheses around the tests in some statements, semicolons at the end of each statement, and braces around a nested block of code.

The end of a line terminates the statement that appears on that line. Alternatively, if more than one statement appears on the same line, they can be terminated with semicolons; similarly, if a statement spans many lines, you must terminate it by closing a bracketed

Return Main Page Previous Page Next Page

®Online Book Reader