Home CPSC 110

Making Decisions

Overview

So far, our programs have started on the first line and executed each line in sequence until the end of the program. However, many algorithms have the need to make decisions and do different things at different times.

For example in this algorithm for guessing the number a user is thinking of, we must do something different if we get the number right vs. if we get it wrong:


1. Write down the numbers 1 to 100 in some order.
2. Guess the next number on the list.
3. If correct, stop.
4. Otherwise, cross off the number we guessed.
5. Repeat Step 2.

Today we will learn how to make programs that do different things based on what happens in a program.


Booleans Types

Before talking about how to make decisions in programs, we must talk about one more type of data. It is called the boolean type. Numbers and strings can have many, many different values, but a boolean can only have 2: True or False.

Boolean values are used to represent truth values: whether something is True or False. They are named after English mathematician George Boole who developed a form of math based on True/False values which became influential in the development of computers.

In the guess the number algorithm, whether we guessed right can be a boolean. If we guessed right, it's True, otherwise it's False.

To make a boolean, we can just assign a variable the values True or False. For instance, we could set whether we got the guess correct to False like this:


correct = False

For another example, we can make a variable for whether or not it is snowing, and assign it the value False:


snowing = False

We could also make a variable for whether it's raining and give it the value True:


raining = True

Booleans are used for keeping track of information like whether certain conditions have been met, or whether events have occurred.


If Statements

In order to actually make decisions based on our booleans, we will use the if statement. If statements in Python look like the following:


if condition:
    line 1
    line 2
    ...

When Python executes this code it will check if the condition is True. If it is, Python will execute each line underneath. If the condition is False, the lines will not be executed.

For example, we could check our raining condition and do something only in that case:


raining = True

if raining:
    print("It is raining.")
    print("Bring an umbrella!")

If we run the program as it is, it will print out our two messages. If we change the variable raining to False, then it won't print them out any more.


Indentation and Spacing

Python uses indentation to mark if lines are part of an if statement or not. For example, we can add another message to the program above, but not indent it:


raining = True

if raining:
    print("It is raining.")
    print("Bring an umbrella!") 
print("Bye bye!")

This program will always print the "Bye bye!" message because it is not indented. Only the first two prints are part of the if block.

For this to work, we need to indent our code the same amount each time. Python accepts spaces or tabs, as long as it is done consistently. But if you mix up indentation, Python will give errors, like this:


raining = True

if raining:
    print("It is raining.")
  print("Bring an umbrella!") 
print("Bye bye!")

Here Python will be confused because it doesn't know what to do with the second message. Is it part of the if block or not?

IDLE handles indentation using tabs and automatically inserts them after a ":" character.


Comparisons

While you can make boolean variables and set them to True or False, it's even more common to get a condition by using a comparison operator. Python provides the following comparison operators:

<Less than.
>Greater than.
<=Less than or equal to.
>=Greater than or equal to.
==Equal to.
!=Not equal to.

These allow us to compare different things and decide what to do based off of that.

For instance, if we want to read in a users age, and make sure that it is a positive number, we could do it with a comparison:


age = int(input("How old are you? "))

if age < 0:
    print("Hey, your age can't be negative!")

What's happening here is that the variable age and the number 0 are compared to see if age is less. If it is, then the condition is True. Otherwise it is False. That controls whether we do the print message or not.


Example: Temperature Advisory

How could we use conditions and if statements to write a temperature advisory program? The goal of the program is to ask the user what the temperature is. If it's less than 15°, we should print a cold warning. It it's over 96°, then we should print a heat warning.


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

if temperature < 15:
    print("Warning: It's very cold!")

if temperature > 96:
    print("Warning: It's very hot!")

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