Making Decisions

Computers make decisions about what to do next by asking questions. These questions usually involve comparing two values. For instance, is one number bigger than the other? If it is, the computer might skip a block of code that it would otherwise have run.


Boolean Values

Create a new file called boolean_example. Let's now create two integer variables, monsters and coins.

The questions that computers ask only have two possible answers: True or False. Python calls these two values Boolean values, and they must always start with a capital letter. You can store a Boolean value in a variable.
Let's now compare our two variables, monsters and coins and store that in a variable called compare:
Notice the symbol we're placing between coins and monsters: >. This symbol means "greater than" and is called a Boolean Operator. Think of it as a math operation like + or -, except it can only be used to equal True or False rather than a number. Below are the other boolean operators and their meanings:

Symbol  Meaning 
> greater than
< less than
== equal to
!= not equal to
*Note that in Python, we use two equal signs (==) to compare, but only one (=) to assign a value to a variable.

Now run the above code and see what output you get. Replace the greater-than symbol (>) with the other boolean operators and see what gets printed


Boolean Expressions

The above code we ran included a Boolean Expression. A boolean expression is a comparison. It contains variables and values and uses logical operators. Can you point out which line of code above is a boolean expression?
In Python, you can also combine more than one comparison by using the logical operators and and or. Add this code to your file:
**When using or, only one of the comparisons needs to be correct for the Boolean value to be True
When using and, both the comparisons need to be correct for the Boolean value to be True

What do and-compare and or-compare equal when you run the code? Play around with the values for monsters and coins, change == to another boolean operator (see table above), and turn and into or and vice versa and see what output you get.

Branching

Sometimes you need to make decisions when playing a game. Should you turn right to investigate the library or turn left to look at the kitchen? Computer programs often contain code that runs only in certain situations. This means the computer makes decisions about which parts of the code to run.

One Branch

The simplest type of branching command is an if statement. It only has one branch, which the computer follows if the condition is True. look at the below example:
In this example, the program checks to see the number of spells you’ve cast. If it’s more than ten, the program prints “You gained the title Enchanter!” If the number of spells you’ve cast is less than ten, the message is not printed. Run the code and see what output you get.
**Play around with the comparison and the value of the spells variable and see how that affects the output.

Two Branches

What if you want the program to do one thing if a condition is True, but another if it’s False? In this case, you need a command with two possible branches, called an if/else statement. look at the below example:
In this example, there’s a variable called game_over, which is set to True. The if statement checks to see if game_over is True. If it is, the program prints “Game Over!” If it isn’t, the else statement runs to print “Keep playing!”
**Try running this code with game_over set to True, then False, and see what output you get.

More Than Two Branches

When there are more than two possible paths, the command elif (short for “else-if”) can be used in your program. In the following example, you need to capture several ghosts in one go. look at the below example:
In this program, the variable ghosts has been set to 3, so the first branch is True and the program prints “It’s so spoooooky!”
But if the value in ghosts was 1, the first branch would be False, so the second branch would run, printing “Get that ghost!”
If neither of the above branches are True, the program moves on to the third branch to print “Ghosts all gone!” An elif statement must always come after if and before else.
Run the above code and see what output you get. Play around with the value for the ghosts variable and the conditions.

Copyright © KX Technology Group, LLC. All Rights Reserved.