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
OUTPUT "Suitable standard."
ELSE
OUTPUT "Do a retest."
ENDIF
END
Tasks
In the code above, is the condition in the if statement True or False?
Give a value for
score
in 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."
?
if score >= cut_off_score
becomes if 65 >= 60:
score
in the code above such that the output is "Do a retest."
."Do a retest."
?"Suitable standard."
?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
OUTPUT "A won."
ELSEIF scoreB > scoreA THEN
OUTPUT "B won."
ELSE
OUTPUT "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
scoreB
in the code above such that the output is"B won."
.Give a value for
scoreB
in 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 = 89scoreB
in the code above such that the output is "A drew with B."
.scoreB == scoreA
so scoreB = 88. e.g. scoreB = 88Tasks
Write python code for the following pseudocode for travelling to school.
BEGIN is_raining ← TRUE IF is_raining THEN OUTPUT "Catch the bus." ELSE OUTPUT "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
OUTPUT "Good Morning."
IF is_raining AND is_cold THEN
OUTPUT "Bring Umbrella and jacket."
ELSE
OUTPUT "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_raining
toFalse
result in a change in the output?Would changing
is_raining
toFalse
andis_cold
toFalse
result in a change from the original output?
is_raining AND is_cold
is True since both is_raining``and ``is_cold
are True.is_raining
to False
result in a change in the output?is_raining
to False
and is_cold
to False
result in a change from the original output?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
OUTPUT "Good Morning."
IF is_raining OR is_cold THEN
OUTPUT "Bring Umbrella or jacket or both."
ELSE
OUTPUT "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_raining
toTrue
result in a change in the output?Would changing
is_raining
toTrue
andis_cold
toTrue
result in a change from the original output?
is_raining OR is_cold
is False since both is_raining``and ``is_cold
are False.is_raining
to True
result in a change in the output?is_raining
to True
and is_cold
to True
result in a change from the original output?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
OUTPUT "Good Morning."
IF is_raining AND is_cold THEN
OUTPUT "Bring Umbrella AND jacket."
ELSEIF is_raining AND NOT (is_cold) THEN
OUTPUT "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 AND jacket."
?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 OUTPUT "Good Morning." IF is_raining AND is_cold THEN OUTPUT "Bring Umbrella and jacket." ELSEIF is_raining AND NOT(is_cold) THEN OUTPUT "Bring Umbrella." ELSEIF NOT(is_raining) AND is_cold THEN OUTPUT "Bring Jacket." ELSE OUTPUT "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
OUTPUT "A won easily."
ELSE
OUTPUT "A won."
ENDIF
ELSEIF scoreB > scoreA THEN
IF scoreB - scoreA > 14 THEN
OUTPUT "B won easily."
ELSE
OUTPUT "B won."
ENDIF
ELSE
OUTPUT "A drew with B."
ENDIF
END
Tasks
Using python code, add the variables
teamA
andteamB
and 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 (myteam + “ 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
OUTPUT ("The " + teamA + " won easily by " + margin + ".")
ELSE
OUTPUT ("The " + teamA + " won by " + margin + ".")
ENDIF
ELSEIF scoreB > scoreA THEN
margin = str(scoreB - scoreA)
IF scoreB - scoreA > 14 THEN
OUTPUT ("The " + teamB + " won easily by " + margin + ".")
ELSE
OUTPUT ("The " + teamB + " won by " + margin + ".")
ENDIF
ELSE
OUTPUT ("The " + teamA + " drew with the " + teamB + ".")
ENDIF
END