5.8. Nested Conditionals

By using an if/else statement, we produce two branches for our code to follow. We can add more branches to our program flow by combining conditionals.

Let’s see how this works by solving the following problem:

Example

Write a program that:

  1. Prompts the user to enter a whole number.

  2. If the number is odd, print nothing.

  3. If the number is even, print “EVEN!” If the number is also positive, print “POSITIVE”.

Our first attempt at a solution might look like this:

1
2
3
4
5
6
7
entry = int(input('Enter a whole number: '))

if entry%2 == 0:
   print("EVEN!")

if entry > 0:
   print("POSITIVE")

Console Output

Enter a whole number: 7

POSITIVE

When we enter 7 in the prompt, we want the program to print nothing. However, we see the output POSITIVE. The code doesn’t work as we want. Why not?

Written this way, the two conditionals are separate from each other. The result from one has no influence on the other. Checking entry as even or odd works fine on its own. However, the second check gets carried out whether or not entry%2 == 0 is True or False. Remember, the if condition does not apply after the first unindented line.

We want to check if entry is positive ONLY IF the number is even. To do this, we need to put the second conditional inside the first. This code structure is called a nested conditional. The result of the first conditional determines whether or not to consider the second.

Try It!

Run this code several times and choose different values for entry (even, odd, positive, negative) to prove that it works as desired. Nice, huh?

Try removing the indentation for lines 6 & 7 to see how the output changes!

Notice that when we put one conditional inside another, the body of the nested conditional is indented by two levels rather than one. In Python, the indentation of an if statement determines if it is nested. For an if statement to run inside another, it must be indented more than the outer conditional.

5.8.1. Nesting Also Works With else

In the examples above, we left out the else clauses from the conditionals. Let’s take nesting a step further by including else clauses:

Try It!

PREDICT: What will this program print for the following words?

  1. the

  2. kale

  3. rutabaga

DISCOVER: Run the program to check your predictions.

Tip

In Python, the amount of indentation tells us exactly which else clause belongs to which if statement.

This diagram shows the flow of control for this program:

A diagram showing how the flow of a program branches based on the value of the condition in a nested if-else statement.

5.8.2. Check Your Understanding

Question

What is printed when the following code runs?

1
2
3
4
5
num = 7

if num % 2 == 0:
   if num % 2 == 1:
      print("odd")
  1. The code won't run due to invalid syntax.
  2. odd
  3. even
  4. The code runs but doesn't print anything.

Question

What is printed when the following code runs?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
answer_1 = 'yes'
answer_2 = 'no'


if answer_1 == 'yes':
   if answer_2 == 'yes':
      print("Both of you agree!")
   else:
      print("You two need to work this out.")
else:
   if answer_2 == 'yes':
      print("Stop arguing and work it out.")
   else:
      print("Clean your bathroom anyway!")
  1. Both of you agree!
  2. You two need to work this out.
  3. Stop arguing and work it out.
  4. Clean your bathroom anyway!