Many times the program's exact change in state depends on a condition. Conditional processing is done by setting statements apart in suites with conditions attached to the suites. The Python syntax for this is an if statement.
The basic form of an if statement provides a condition and a suite of statements that are executed when the condition is true. It looks like this:
if expression : suite
The suite is an indented block of
statements. Any statement is allowed in the block, including indented
if statements. You can use either tabs or spaces for
indentation. The usual style is four spaces.
This is our first compound statement. See Python Syntax Rules for some additional guidance on syntax for compound statements.
The if statement evaluates the condition
expression first. When the result is
True, the suite of
statements is executed. Otherwise the suite is skipped.
For example, if two dice show a total of 7 or 11, the throw is a winner.
d1 and d2 are two dice
if d1+d2 == 7 or d1+d2 == 11:
print "winner", d1+d2
Here we have a typically complex expression. The
or operator evaluates the left side first. Python
evaluates and applies the high-precendence arithmetic operator before
the lower-precendence comparison operator. If the left side is true
(d1+d2 is 7), the
or expression is true, and the suite is executed. If
the left side is false, then the right side is evaluated. If it is true
(d1+d2 is 11), the
or expression is true, and the suite is executed.
Otherwise, the suite is skipped.
Often there are several conditions that need to be handled. This is done by adding elif clauses. This is short for “else-if”. We can add an unlimited number of elif clauses. The elif clause has almost the same syntax as the if clause.
elif expression : suite
Here is a somewhat more complete rule for the come out roll in a game of craps:
result= None
if d1+d2 == 7 or d1+d2 == 11:
result= "winner"
elif d1+d2 == 2 or d1+d2 == 3 or d1+d2 == 12:
result= "loser"
print result
First, we checked the condition for winning; if the first condition is true, the first suite is executed and the entire if statement is complete. If the first condition is false, then the second condition is tested. If that condition is true, the second suite is executed, and the entire if statement is complete. If neither condition is true, the if statement has no effect.
Python also gives us the capability to put a “catch-all” suite at the end for all other conditions. This is done by adding an else clause. The else clause has the following syntax.
else: suite
Here's the complete come-out roll rule, assuming two values
d1 and d2.
point= None
if d1+d2 == 7 or d1+d2 == 11:
print "winner"
elif d1+d2 == 2 or d1+d2 == 3 or d1+d2 == 12:
print "loser"
else:
point= d1+d2
print "point is", point
Here, we use the else: suite to handle all of the other possible rolls. There are six different values (4, 5, 6, 8, 9, or 10), a tedious typing exercise if done using or. We summarize this with the else: clause.
While handy in one respect, this else: clause is also dangerous. By not explicitly stating the condition, it is possible to overlook simple logic errors.
Consider the following complete if statement that checks for a winner on a field bet. A field bet wins on 2, 3, 4, 9, 10, 11 or 12. The payout odds are different on 2 and 12.
outcome= 0
if d1+d2 == 2 or d1+d2 == 12:
outcome= 2
print "field pays 2:1"
elif d1+d2==4 or d1+d2==9 or d1+d2==10 or d1+d2==11:
outcome= 1
print "field pays even money"
else:
outcome= -1
print "field loses"
Here we test for 2 and 12 in the first clause; we test for 4, 9, 10 and 11 in the second. It's not obvious that a roll of 3 is missing from the even money pay out. This fragment incorrectly treats 3, 5, 6, 7 and 8 alike in the else:. While the else: clause is used commonly as a catch-all, a more proper use for else: is to raise an exception because a condition was not matched by any of the if or elif clauses.