Decision making is an essential part of most applications. IronPython doesn’t provide the wide range of decision-making options that other languages do. In fact, you have just one decision-making structure: the if statement. Fortunately, the if statement takes three forms that meet every possible programming need, even when using the if statement isn’t quite as elegant as other possibilities, such as Select…Case. The following sections describe the three forms of the if statement:
- if
- if…else
- if…elif…else
Performing a Simple Decision Using if
The simplest kind of decision is one where you look for a particular value or range of values and then perform a task when you find what you’re looking for. For example, you might expect the user to input a certain value and then test for that value in your application, as shown in Listing 3-2. Like every other computer language on the planet, the expression provided for an IronPython if statement is a Boolean value of some type.
Listin g 3-2: Making a simple decision
# Define an input variable.
Answer = raw_input(‘Say hello (Y/N)?’)
# Check the value of Answer.
if Answer.upper() == ‘Y’:
# Perform some tasks based on a positive response.
print ‘You typed:’, Answer
print ‘So Hello!’
# Pause after the debug session.
raw_input(‘Press any key to continue…’)
The code for this example retrieves some input from the user and reacts to it. You’ve probably created many applications that do precisely the same thing. In this case, the application uses raw_input() to obtain the input from the user. You can see how the prompt looks in Figure 3-2.
At this point, the code creates the if statement. Notice that the if statement ends with a colon (:) just as all IronPython statements do. Everything that’s indented after the if statement is part of the structure. There aren’t any opening or closing statements, just the colon and the statements you want to execute (making IronPython one of the least cluttered languages available). Figure 3-2 shows the output from the application when the user provides either a y or Y as input.
One of the most common errors that developers who are familiar with other languages make when working with IronPython is to forget to include the colon after a structural element. Unfortunately, the error message doesn’t always tell you that the colon is missing — it might point you in some completely different direction. If you get an error message that doesn’t make sense, you might want to check for a missing colon in your code. In fact, have someone else look for the missing colon when you can’t find it — you’ll be amazed at how often a little colon causes you all kinds of woe.
One of the interesting features of this example is that Answer is actually an object. You can’t declare it as an object or any data type at all. In fact, the code simply assigns a string to Answer and everything happens in the background. You can find a whole list of string methods at http://docs.python.org/library/stdtypes.html#string-methods. This book shows how to use a number of these methods. For now, just keep in mind that IronPython tends to hide complexity in ways that other languages don’t.
Choosing between Two Options Using if . . . else
Sometimes you need to do more than simply decide to do something based on the output of a Boolean expression — you also need to do something if the expression is false. Most programming languages handle this using some form of if…else structure, which is precisely what IronPython does. Listing 3-3 adds to the example shown in Listing 3-2 by doing something when the reader fails to provide the expected input.
Listin g 3-3: Making an either/or decision
# Define an input variable.
Answer = raw_input(‘Say hello (Y/N)?’)
# Check the value of Answer.
if Answer.upper() == ‘Y’:
# Perform some tasks based on a positive response.
print ‘You typed:’, Answer
print ‘So Hello!’
# The user must have typed something else.
else:
# Perform some tasks based on a different response.
print ‘Sorry, you typed:’, Answer, ‘and not Y.’
# Pause after the debug session.
raw_input(‘Press any key to continue…’)
The big thing to notice in this example is that you outdent the else clause and follow it with a colon. Every indented statement after the else clause is part of the else portion of the structure. The output from this example is similar to the output shown in Figure 3-2. The application asks a question and then outputs something based on the response the user provides.
Creating a Decision Tree Using if . . . elif . . . else
Many programming languages provide a special structure to handle complex decisions. IronPython As you might expect, each elif clause includes a Boolean expression. In many respects, the elif clauses act as the case clauses for the Select…Case structure. Of course, you gain some flexibility because the elif clause need not match a particular variable. The else clause acts like the default clause for the Select…Case structure. Listing 3-4 shows how to work with the elif clause.
Listin g 3-4: Making complex decisions
# Define an input variable.
Answer = raw_input(‘Say hello (Y/N)?’)
# Check the value of Answer.
if Answer.upper() == ‘Y’:
# Perform some tasks based on a positive response.
print ‘You typed:’, Answer
print ‘So Hello!’
# Check to see if the user entered N
elif Answer.upper() == ‘N’:
# Give a reponse for a negative answer.
print ‘Sorry to hear you don‘t want to say hello.’
# The user must have typed something else.
else:
# Perform some tasks based on a different response.
print ‘You need to type Y or N!’
# Pause after the debug session.
raw_input(‘Press any key to continue…’)
As you can see from the code, the elif clause looks very much like the if clause. The only difference is that the elif clause must follow the if clause. You can include as many elif clauses as required to perform a particular task. The output from this example is similar to that shown in Figure 3-2.