An introduction to the use of Selection in Python
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:
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:
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:
Write a program that:
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.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
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.