More Complicated if-elif-else Statements

2.7. More Complicated if-elif-else Statements#

2.7.1. Combining conditions#

Don’t forget you can make more complex conditions by combining multiple conditions using and and or.

food = 'steak'
side = 'salad'

if food == 'fish' and sides == 'chips':
    print('Fish and chips! My favourite!')
else:
    print('Enjoy your {} and {}!'.format(food, side))
../../_images/4_example1.png

2.7.2. Nesting#

You can also put if-elif-else statements inside other if-elif-else statements. Pay close attention to the indentation.

In this example, if the first condition evaluates to True the code is directed to another if-else statement.

x = 4

if x % 2 == 0:
    if x % 3 == 0:
        print("{} is divisible by both 2 and 3".format(x))
    else:
        print("{} is divisible by 2 but not by 3".format(x))
elif x % 3 == 0:
    print("{} is divisible by 3 but not by 2".format(x))
else:
    print("{} is not divisible by either 2 or 3".format(x))
../../_images/4_example2.png
Question 1

What do you think the output of the following code will be?

today = 'Wednesday'
holidays = False

if holidays:
    print("It's the holidays!")
else:
    if today == 'Saturday' or today == 'Sunday':
        print("It's the weekend!")
    else:
        print('Today is a school day.')
Solution
Today is a school day.

The first condition evaluates to False, so we go down the else branch. Inside this branch we have another if-else statement. This condition also evaluates to False, so the program will print Today is a school day.

../../_images/4_question1.png
Question 2

Consider the two programs below. They are similar but not quite the same. If you set red = 3 and blue = 2, both programs will output

Red team wins!
Congratulations to the winning team!

Program 1

red = 3
blue = 2

if red > blue:
    print('Red team wins!')
elif blue > red:
    print('Blue team wins!')
else:
    print("It's a draw!")
print('Congratulations to the winning team!')

Program 2

red = 3
blue = 2

if red == blue:
    print("It's a draw!")
else:
    if red > blue:
        print('Red team wins!')
    else:
        print('Blue team wins!')
    print('Congratulations to the winning team!')

Suppose we changed the value stored in the variables red and blue. For which of the following will program 1 and program 2 result in different outputs? Select all that apply.

  1. red = 5
    blue = 0
    
  2. red = 2
    blue = 2
    
  3. red = 2
    blue = 4
    
  4. red = 0
    blue = 0
    
Solution

Solution is locked

Code challenge: Red Team Blue Team

Implement the algorithm illustrated in the diagram below in Python. Pay close attention to the indentation.

../../_images/4_question2.png
Solution

Solution is locked

Code challenge: Order x, y and z

Write a program that asks the user for integers x, y and z. Each value will be unique, i.e. none of the values will be the same. Your program should then order these values from largest to smallest and display them in the following format:

largest > second largest > smallest

Example 1

x: 3
y: 7
z: 1
7 > 3 > 1

Example 2

x: 2
y: 5
z: 9
9 > 5 > 2

Example 3

x: 6
y: 4
z: 8
8 > 6 > 4
Solution

Solution is locked