Elif
Elif
Passed or Failed
Successfully Login or Incorrect
Password
stars >= 5
To program selection in Python we use the key words if, elif and else.
elif means ‘else if’. For every if statement, we can include any
number of elif statements. else can be thought of as meaning
‘none of the above’. For every if statement, we can include a single
else statement, or no else statements (else is optional).
Key Terms
Indentation is very important in Python. Each block of code after
an if, elif or else statement must be indented.
There should always be a colon : at the end of every line of code
containing if, elif or else statements. Python IDLE will then automatically
indent the next line of code when we use a colon correctly.
if something == True:
# stuff that happens if something is true
elif something else == True:
# stuff that happens if something else is true
else:
# stuff that happens if none are true
When Programs Go Wrong?
Pause for Thought
What would happen if your forgot to include a colon at the end
of an if statement? Answer: it would produce a syntax error.
Try it now and see for yourself.
Do some research to find out what a syntax error is.
How does Python IDLE help you find and fix syntax errors?
Key Terms
An error in a computer program is known as a bug. Try and
find out why. The process of finding and then fixing errors
in computer programs is often called debugging. Many IDEs
such as Python IDLE have inbuilt debugging tools.
Hands On Activity
╸ Weather Forecast Program
1. Let the user enter temperature in Celsius.
2. Evaluate the input if it is Cold, Cool, Warm or Hot
based on the given range:
0 - 10 Cold 20 - 25 Warm
11- 19 Cool 26 and above - Hot
3. Display the remarks.
Hands On Activity
╸ Modified Python Calculator
A. Let the user enter two numbers.
B. Display the operations to choose from.
1 = Addition 3 = Multiplication 5 = Remainder
2 = Subtraction 4 = Division
C. Let the user choose among the options.
D. Calculate the answer based on the chosen operation.
E. Display the answer.
Thank you!
15