Home CPSC 110

Making Decisions Continued

Today we will look at more ways of writing programs that take different actions based on their input.

Boolean Operations

Sometimes we want to combine conditions. Like if we want to see if it's a nice temperature, we would need to make sure the temperature is above a certain point and also below a certain point.

We can do this with the boolean operators.

Booleans can't be added or subtracted, but there are three operators that work with them. They are and, or and not.

and takes 2 booleans and tells is if they are both True. For example, if we want to see if it's raining and snowing, we could use the following:


>>> raining = True
>>> snowing = False
>>> raining and snowing
False

Here only one was True, not both, so the result of the and is False. If either one is False, or both are False, and returns False. Only when both are True does and give True:


>>> False and False
False
>>> True and False
False
>>> False and True
False
>>> True and True
True

or works similarly, except it tells us if either thing is True. If we ask if it's snowing or raining (based on our variables above), we will get True:


>>> raining or snowing
True

If either of the two things are True, or gives us True. If they are both False, it will give False:


>>> False or False
False
>>> True or False
True
>>> False or True
True

If both things are True, then or returns True as well. This is sometimes different from how we use the word "or" in English. For instance if you can choose soup or salad with a meal, you usually can't choose both (without paying extra anyway). In computer science, "or" means "either one, or the other, or both":


>>> True or True
True

The last boolean operator is not. This just takes one boolean and gives you the opposite of it. For example, if we want to check if it is not raining, we could do so like this:


if not raining:
    print("No umbrella needed!")


Using Multiple Conditions

We can thus combine up multiple conditions with these boolean operators. For example, if we want to see if the temperature is in a nice range, we could use and:


temperature = int(input("What is the temperature? "))

if temperature > 50 and temperature < 85:
    print("It's a nice temperature!")

We can combine up as many conditions as we want. We can also use parenthesis to group things. For instance, this rather complicated condition checks multiple things to decide if it's a nice day or not:


raining = True
snowing = False
temperature = int(input("What is the temperature? "))

if temperature > 50 and temperature < 85 and not (raining or snowing):
    print("It is a nice day!")


Two-Way Decisions

Oftentimes, we want to test a condition and, if it's true, do one thing, and if it's false, do another thing. For example, if we want to print whether a message based on whether it's raining we could do this:


raining = True

if raining:
    print("Bring an umbrella!")

if not raining:
    print("No need for an umbrella.")

However, because this is so common, there is a simpler way to do this using the "else" keyword:


raining = True

if raining:
    print("Bring an umbrella!")
else:
    print("No need for an umbrella.")

With an if/else statement, if the if condition is true, than the code under the if line is executed. Otherwise, the code under the else line is executed instead.

It can never do both things.


Example: Reading in Booleans

In the weather checker program above, raining and snowing are hard-coded. It would be nice to read them from the user instead.

Python doesn't really provide a built-in way to read in boolean values. Instead what we will do is to use input to ask a question. Then we will check if the user types "yes" or "no" and set a variable based on that.

How could we write a program which asks whether it's raining, whether it's snowing, and sets variables based on that?


answer = input("Is it raining (yes/no)? ")
if answer == "yes":
    raining = True
else:
    raining = False

answer = input("Is it snowing (yes/no)? ")
if answer == "yes":
    snowing = True
else:
    snowing = False

Multi-Way Decisions

We can also write if statements with more than 2 options using the "elif" keyword. This stands for "else if". When Python executes a multi-way decision, it checks each condition in order. Once it finds one condition that is true, it executes the statements under it and then goes to the end of the chain.

For example, we can write a program which will print messages based on the temperature with three cases like this:


temperature = int(input("What is the temperature? "))

if temperature < 50:
    print("It's cold!")
elif temperature < 80:
    print("It's medium.")
else:
    print("It's hot!")

In this program, it will always print exactly one of these messages. The conditions are checked one by one. As soon as one is found to be true, Python will execute the code under it and stop.

For example, if the temperature is 40, then it will find the first condition to be true and print "It's cold!". Even though the second condition is also true, it won't print any other message.

There's no limit to how many elif statements we can include.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.