IF...elif ...else
Learning Objectives:
Identify - that IF and ELSE are limited to only 2 possible outcomes
Describe - how ELIF can be used to extend options beyond just IF and ELSE
Explain - in a flowchart how the IF...ELIF ...ELSE can be used in a program
Apply- a range of conditional statements based on the ELIF structure
Link - your new ELIF skills into real code that works in a Python program
We know how to use an if statement to set two conditions, but what if we want more than two options?
The answer is: elif.
It allows you to extend your choices to more than just the two available with if else. Now you can have:
if ...junk Food..."mmm keep it coming"
elif ...Doughnuts..."arggg drooool"
elif...Sweets..."Its not doughnuts but mmmm"
elif...Duff..."Mhmmmmm"
elif (as many as you need - which for Homer is a lot!)
else..."I can't eat this!!!"
It allows you to extend your choices to more than just the two available with if else. Now you can have:
if ...junk Food..."mmm keep it coming"
elif ...Doughnuts..."arggg drooool"
elif...Sweets..."Its not doughnuts but mmmm"
elif...Duff..."Mhmmmmm"
elif (as many as you need - which for Homer is a lot!)
else..."I can't eat this!!!"
Adventure Game Example:
Note:
- You can add as many elif's as you want!
- Remember to use 3 lots of " ("""string""") to write strings on multiple lines
- Use \n for a new Line to escape and start a new line without ending the string
- it is possible for the user to type something other than a, b or c. In this case it would go to the else command.
challenge:
- Draw a flow chart for your own adventure game that uses IF...ELIF...ELSE
- Where could you use the code: input("Press any key to stand up")
- Code the adventure in Python!
NEED HELP?
EXTENSION: |
|