Selection With Three Or More Outcomes
Learning Objectives/Goals
Be able to read, comprehend, trace, adapt and create Python code using selection that:
- Uses Boolean conditions
- Uses selection using IF, ELIF and ELSE for more than two situations
This task contains code examples.
Look at each example , study it carefully. Write a prediction of what it will do when it runs. Your prediction should be added to the code as comments. You should use the key terms selection, condition and branch in your prediction.
Conditions and their definitions
This is a list of definitions:
if condition:
Code to run if a certain condition happens.
elif condition:
Code to run if condition above is false.
else:
Code to run if all conditions have been checked and found false.
Lesson slides to introduce concepts and accompany these activities are here
Help! My Code Does Not Work!
Make sure that you check for the following things:
- selection begins with ‘if’ has as many ‘elif’s in the middle as you need and end with ‘else’
- a colon at the end of each if and else line.
- each branch is indented unsing the TAB key (not just spaces)
- if, elif & else aren’t indented
- a condition after each ‘if’ and ‘elif’
- no condition after ‘else’
- a double equals in the condition when chcking that two pieces of data are the same.