5. Selection
if … elif … else.and, or, and not increase the options for testing conditions.Python |
Pseudocode |
|---|---|
= |
← |
if |
IF ….THEN |
elif |
ELSEIF ….THEN |
else |
ELSE |
“end of if” |
ENDIF |
or |
OR |
and |
AND |
not |
NOT |
True |
TRUE |
False |
FALSE |
5.1. If, else
if is used with a condition that results in True or False.if statement is executed.False, else: can be used to execute other code.score = 65
cut_off_score = 60
if score >= cut_off_score:
print("Suitable standard.")
else:
print("Do a retest.")
BEGIN
score ← 65
cut_off_score ← 60
IF score >= cut_off_score THEN
PRINT "Suitable standard."
ELSE
PRINT "Do a retest."
ENDIF
END
Tasks
In the code above, is the condition in the if statement True or False?
Give a value for
scorein the code above such that the output is"Do a retest.".Assuming scores can only be integers from and including 0 to 100, how many different scores would result in the output of
"Do a retest."?Assuming scores can only be integers from and including 0 to 100, how many different scores would result in the output of
"Suitable standard."?
In the code above, is the condition in the if statement True or False?
``if score >= cut_off_score`` becomes ``if 65 >= 60:``
This evaluates to True
Give a value for score in the code above such that the output is "Do a retest.".
"Do a retest." is in the False branch.
So the score is below 60. e.g. 59
"Do a retest."?Required scores are from 0 to 59.
There are 60 scores.
"Suitable standard."?Required scores are from 60 to 100.
There are 41 scores.
5.2. If, elif, else
elif with a condition.:, at the end of each if, elif and else statement.scoreA = 88
scoreB = 85
if scoreA > scoreB:
print("A won.")
elif scoreB > scoreA:
print("B won.")
else:
print("A drew with B.")
BEGIN
scoreA ← 88
scoreB ← 85
IF scoreA > scoreB THEN
PRINT "A won."
ELSEIF scoreB > scoreA THEN
PRINT "B won."
ELSE
PRINT "A drew with B."
ENDIF
END
Tasks
In the code above, is the condition in the if statement True or False?
Give a value for
scoreBin the code above such that the output is"B won.".Give a value for
scoreBin the code above such that the output is"A drew with B.".
``scoreA > scoreB`` is True since 88 > 85.
scoreB in the code above such that the output is "B won.".``scoreB > scoreA`` so scoreB > 88. e.g. scoreB = 89
scoreB in the code above such that the output is "A drew with B.".``scoreB == scoreA`` so scoreB = 88. e.g. scoreB = 88
Tasks
Write python code for the following pseudocode for travelling to school.
BEGIN is_raining ← TRUE IF is_raining THEN PRINT "Catch the bus." ELSE PRINT "Ride the bike." ENDIF END
is_raining = True
if is_raining:
print("Catch the bus.")
else:
print("Ride the bike.")
5.3. And, or, not
and keyword is a logical operator used to combine conditional statements.if is_raining and is_cold returns True if both are True.if is_raining and is_cold returns False if either are False.is_raining = True
is_cold = True
print("Good Morning.")
if is_raining and is_cold:
print("Bring Umbrella and jacket.")
else:
print("Umbrella and jacket are optional.")
BEGIN
is_raining ← TRUE
is_cold ← TRUE
PRINT "Good Morning."
IF is_raining AND is_cold THEN
PRINT "Bring Umbrella and jacket."
ELSE
PRINT "Umbrella and jacket are optional."
ENDIF
END
Tasks
In the code above, is the condition in the if statement True or False?
What is the expected output from the code above?
Would changing
is_rainingtoFalseresult in a change in the output?Would changing
is_rainingtoFalseandis_coldtoFalseresult in a change from the original output?
``is_raining AND is_cold`` is True since both ``is_raining``and ``is_cold`` are True.
"Bring Umbrella and jacket."
is_raining to False result in a change in the output?Yes, since the condition would evaluate to False instead of True.
is_raining to False and is_cold to False result in a change from the original output?Yes, the output would be: "Umbrella and jacket are optional."
or keyword is a logical operator used to combine conditional statements.if is_raining or is_cold returns True if either is True.if is_raining or is_cold returns False if both are False.is_raining = False
is_cold = False
print("Good Morning.")
if is_raining or is_cold:
print("Bring Umbrella or jacket or both.")
else:
print("Wear a sun hat.")
BEGIN
is_raining ← FALSE
is_cold ← FALSE
PRINT "Good Morning."
IF is_raining OR is_cold THEN
PRINT "Bring Umbrella or jacket or both."
ELSE
PRINT "Wear a sun hat."
ENDIF
END
Tasks
In the code above, is the condition in the if statement True or False?
What is the expected output from the code above?
Would changing
is_rainingtoTrueresult in a change in the output?Would changing
is_rainingtoTrueandis_coldtoTrueresult in a change from the original output?
``is_raining OR is_cold`` is False since both ``is_raining``and ``is_cold`` are False.
"Wear a sun hat."
is_raining to True result in a change in the output?Yes, since the condition would evaluate to True instead of False.
is_raining to True and is_cold to True result in a change from the original output?Yes, the output would be: "Bring Umbrella or jacket or both."
not keyword is a logical operator.is_raining = True
is_cold = False
print("Good Morning.")
if is_raining and is_cold:
print("Bring Umbrella and jacket.")
elif is_raining and not(is_cold):
print("Bring Umbrella.")
BEGIN
is_raining ← TRUE
is_cold ← FALSE
PRINT "Good Morning."
IF is_raining AND is_cold THEN
PRINT "Bring Umbrella AND jacket."
ELSEIF is_raining AND NOT (is_cold) THEN
PRINT "Bring Umbrella."
ENDIF
END
Tasks
In the code above, is the condition in the ELSEIF statement True or False?
What is the expected output from the code above?
What change would be needed in the variable assignments to result in the output being
"Bring Umbrella AND jacket."?
``is_raining AND NOT (is_cold)`` is TRUE since both ``is_raining``and ``NOT (is_cold)`` are True.
"Bring Umbrella."
"Bring Umbrella AND jacket."?Change ``is_cold ← FALSE`` to ``is_cold ← TRUE``.
Tasks
Write python code for the following pseudocode on preparing for the weather.
BEGIN is_raining ← False is_cold ← False PRINT "Good Morning." IF is_raining AND is_cold THEN PRINT "Bring Umbrella and jacket." ELSEIF is_raining AND NOT(is_cold) THEN PRINT "Bring Umbrella." ELSEIF NOT(is_raining) AND is_cold THEN PRINT "Bring Jacket." ELSE PRINT "Wear a sun hat." ENDIF END
is_raining = False
is_cold = False
print("Good Morning.")
if is_raining and is_cold:
print("Bring Umbrella and jacket.")
elif is_raining and not(is_cold):
print("Bring Umbrella.")
elif not(is_raining) and is_cold:
print("Bring Jacket.")
else:
print("Wear a sun hat.")
5.4. Nested if
if statements within if statements.if and the elif below have a nested if and else that are used when their condition is true.scoreA = 38
scoreB = 35
if scoreA > scoreB:
if scoreA - scoreB > 14:
print("A won easily.")
else:
print("A won.")
elif scoreB > scoreA:
if scoreB - scoreA > 14:
print("B won easily.")
else:
print("B won.")
else:
print("A drew with B.")
BEGIN
scoreA ←38
scoreB ← 35
IF scoreA > scoreB THEN
IF scoreA - scoreB > 14 THEN
PRINT "A won easily."
ELSE
PRINT "A won."
ENDIF
ELSEIF scoreB > scoreA THEN
IF scoreB - scoreA > 14 THEN
PRINT "B won easily."
ELSE
PRINT "B won."
ENDIF
ELSE
PRINT "A drew with B."
ENDIF
END
Tasks
Using python code, add the variables
teamAandteamBand make up team names for them. Modify the code to print the team name instead of ‘A’ or ‘B’. Hint: To join text use a plus symbol. e.g (my_team + “ my text”)Using pseudocode, modify the code to include the changes in Q1, and to print the winning margins. Use
str(number)to convert numbers to text for joining with other text. e.g “The Chiefs won by 3.”
teamA = "Chiefs"
teamB = "Eagles"
scoreA = 38
scoreB = 35
if scoreA > scoreB:
if scoreA - scoreB > 14:
print("The " + teamA + " won easily.")
else:
print("The " + teamA + " won.")
elif scoreB > scoreA:
if scoreB - scoreA > 14:
print("The " + teamB + " won easily.")
else:
print("The " + teamB + " won.")
else:
print("The " + teamA + " drew with the " + teamB + ".")
BEGIN
teamA ← "Chiefs"
teamB ← "Eagles"
scoreA ←38
scoreB ← 35
IF scoreA > scoreB THEN
margin = str(scoreA - scoreB)
IF scoreA - scoreB > 14 THEN
PRINT "The " + teamA + " won easily by " + margin + "."
ELSE
PRINT "The " + teamA + " won by " + margin + "."
ENDIF
ELSEIF scoreB > scoreA THEN
margin = str(scoreB - scoreA)
IF scoreB - scoreA > 14 THEN
PRINT "The " + teamB + " won easily by " + margin + "."
ELSE
PRINT "The " + teamB + " won by " + margin + "."
ENDIF
ELSE
PRINT "The " + teamA + " drew with the " + teamB + "."
ENDIF
END