0% found this document useful (0 votes)
9 views15 pages

Elif

The document discusses programming concepts in Python, focusing on making selections based on user input, such as awarding extra lives in a game and evaluating letter grades. It explains the use of conditional statements (if, elif, else) and emphasizes the importance of indentation and syntax in coding. Additionally, it includes hands-on activities for practicing these concepts, such as a weather forecast program and a modified calculator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Elif

The document discusses programming concepts in Python, focusing on making selections based on user input, such as awarding extra lives in a game and evaluating letter grades. It explains the use of conditional statements (if, elif, else) and emphasizes the importance of indentation and syntax in coding. Additionally, it includes hands-on activities for practicing these concepts, such as a weather forecast program and a modified calculator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Our last program only had two choices.

Passed or Failed
Successfully Login or Incorrect
Password

What if we wanted to have more than


two choices?
An extra life is awarded in a computer game if the player
reaches 10,000 points.
points >=10000
An extra life is awarded in a computer game if you collected
greater than or equal to 5 stars.

stars >= 5

An extra life is awarded in a computer game if the player


reaches 10,000 points or game if you collected greater than or
equal to 5 stars.
points >=10000 OR stars >= 5
1. Let the user enter the number of points gained and stars
collected.

points = int(input(“Enter the your points: ”))


stars = int(input(“Enter the number of stars: ”))
2. Evaluate the entered value of points and number of stars
based on the condition. Display “Extra life awarded” if the user
reaches the minimum requirements.
if points >=10000 :
print “Extra life awarded”
elif stars >= 5:
print “Extra life awarded”
else:
print “No extra life awarded”
if points >=10000 or stars >= 5 :
print “Extra life awarded”
else:
print “No extra life awarded”
Practice Activity
Letter grade equivalent from A-E.
1. Let the user enter the needed
value. (number grade)
2. Evaluate the input of the user.
3. Display the remarks.
A* 90 - 100
A 80 - 89
B 70 – 79
C 60 – 69
D 50 – 59
E 40 – 49
9
Key Terms
Selection is when computers make decisions.
A computer can be programmed to make a selection between
two or more choices, depending on the condition (the question).

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?

>>> if something == True


SyntaxError: invalid syntax

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

You might also like