ELSE IF Statement

An introduction to the use of Selection in Python

ELSE IF Statement

So far, we have presented a Boolean option for conditional statements, with each if statement evaluating to either true or false. In many cases, we will want a program that evaluates more than two possible outcomes. For this, we will use an else if statement, which is written in Python as elif. The elif or else if statement looks like the if statement and will evaluate another condition.

In the bank account program, we may want to have three discrete outputs for three different situations:

  • The balance is below 0
  • The balance is equal to 0
  • The balance is above 0
The elif statement will be placed between the if statement and the else statement as follows:

balance = 522

if balance < 0:
    print("Balance is below 0, add funds now or you will be charged a penalty.")

elif balance == 0:
    print("Balance is equal to 0, add funds soon.")

else:
    print("Your balance is 0 or above.")

Now, there are three possible outputs that can occur once we run the program:

  • If the variable balance is equal to 0 we will receive the output from the elif statement (Balance is equal to 0, add funds soon.)
  • If the variable balance is set to a positive number, we will receive the output from the else statement (Your balance is 0 or above.).
  • If the variable balance is set to a negative number, the output will be the string from the if statement (Balance is below 0, add funds now or you will be charged a penalty).
What if we want to have more than three possibilities, though? We can do this by writing more than one elif statement into our code.

In the grade program, let’s rewrite the code so that there are a few letter grades corresponding to ranges of numerical grades:

  • 90 or above is equivalent to an A grade
  • 80-89 is equivalent to a B grade
  • 70-79 is equivalent to a C grade
  • 65-69 is equivalent to a D grade
  • 64 or below is equivalent to an F grade
To run this code, we will need one if statement, three elif statements, and an else statement that will handle all failing cases.

Let’s rewrite the code from the example above to have strings that print out each of the letter grades. We can keep our else statement the same.

grade = 60

if grade >= 90:
    print("A grade")

elif grade >=80:
    print("B grade")

elif grade >=70:
    print("C grade")

elif grade >= 65:
    print("D grade")

else:
    print("Failing grade")

Since elif statements will evaluate in order, we can keep our statements pretty basic. This program is completing the following steps:

  • If the grade is greater than 90, the program will print A grade, if the grade is less than 90, the program will continue to the next statement...
  • If the grade is greater than or equal to 80, the program will print B grade, if the grade is 79 or less, the program will continue to the next statement...
  • If the grade is greater than or equal to 70, the program will print C grade, if the grade is 69 or less, the program will continue to the next statement...
  • If the grade is greater than or equal to 65, the program will print D grade, if the grade is 64 or less, the program will continue to the next statement...
  • The program will print Failing grade because all of the above conditions were not met.

Introduction

Write a program that:

  1. Write an input line to ask a user whether they want to take the red pill or the blue pill.
  2. If they write “red” then print “You stay in wonderland and see how far the rabbit hole goes”.
  3. Elif they write “blue” then print “You wake up in your bed and believe what you want to believe.”.
  4. Else print “That’s not an option Neo.”

Write a program that creates a multiple choice quiz of 4 possible answers.

Write a program that asks for a pupil score and outputs a suitable grade and comment.

9.3 ELSE IF Statement Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.