Combining tests

You don't always have to write elif statements when you want to test multiple conditions. If you want to perform the same action in more than one condition, you can combine the conditions in a single if or elif statement.

The code snippets in Listings 10-1 and 10-2 both test for the same conditions, but the example combining multiple conditions in the if statement is easier to read:

Listing 10-1: elif block_

print "Discount rate" elif age > 65:

print "Discount rate" else:

_print "Adult rate"_

Listing 10-2: Two conditions in an if statement if (age < 18) or (age > 65):

print "Discount rate" else:

print "Adult rate"

Was this article helpful?

0 0

Post a comment