Frequently we want to make more complex branching decisions,
involving two or more conditions. For example: if you start
the labs early and you study for the quizzes you will do
well in this class. Notice how those two conditions (start labs
early, study for quizzes) are combined with the and
operator.
If both conditions are True, then the combined condition is
also True.
Combining conditions with and
and or
, or nesting the if
statements
are two ways to create more complex branching decisions.
For the and
and or
logical operators, you can simply combine
two conditions like this:
if some-condition-is-True and some-other-condition-is-True:
do this block of code
else:
do this block of code
if some-condition-is-True or some-other-condition-is-True:
do this block of code
else:
do this block of code
You could also use nested if statements to get the same effect
for the two conditions joined with the and
operator:
if some-condition-is-True:
if some-other-condition-is-True:
do this block of code
# this inner block only executed if both conditions True!
else:
do this block of code
else:
do this block of code
As before, a block of code can be one or more lines, and must all be indented.
Again, using and
between two conditions requires both to be True
for the combined condition to be True
. Using or
requires either
(or both) to be True
. The not
operator just negates the condition.
Here's a table showing two conditions, A and B, their possible values (both True, one True the other False, etc), and the values of the combined conditions:
A B A and B A or B not A
---- ---- ------- ------ -----
True True True True False
True False False True False
False True False True True
False False False False True
Here's a simple test to see if input from the user is in range:
userchoice = int(raw_input("Please enter a number from 1-10: "))
if userchoice >= 1 and userchoice <= 10:
print("Thank you!")
else:
print("That's not in range!!! :( ")
You could also check if the input is not out of range:
if not (userchoice < 1 or userchoice > 10):
print("Thank you!")
else:
print("That's not in range!!! :( ")
That seems klunky, but it does work. Notice the and
changed to an
or
, the comparison operators changed (greater-than-or-equal to
less-than, etc), and we added the not
or negative to the whole thing.
You could also do the above using nested statements:
if userchoice >= 1:
if userchoice <= 10:
print("Thank you!")
else:
print("That's too high!!! :( ")
else:
print("That's too low!!! :( ")
That's more lines of code, but it also gives the user a more precise error message.
Write a speeding ticket calculator, with these three cases:
If you are caught speeding where the posted speed limit is >= 65 mph, your fine is $33 plus $2 for every mph over the speed limit.
If you are caught speeding where the posted speed limit is less than 65 mpg, your fine is $25 plus $2 for every mph over the speed limit.
$ python ticket.py
What is the speed limit in mph? 70
How fast were you going in mph? 68
Safe driving!
$ python ticket.py
What is the speed limit in mph? 70
How fast were you going in mph? 72
A little fast, but no ticket.
$ python ticket.py
What is the speed limit in mph? 70
How fast were you going in mph? 80
You were speeding and your fine is $53