CASE as if/elif/else in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

CASE as if/elif/else in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Python has no CASE statement. How do you write CASE OF Move in Python?
A An if/elif/else chain that tests Move each time
B A CASE OF ... ENDCASE block, exactly as in pseudocode
2. Which keyword does the FIRST value of a CASE OF Move become in Python?
A elif Move == "L":
B if Move == "L":
3. Which line is the correct Python for the second value in a CASE OF Move?
A elif Move == "R":
B elif Move = "R":
4. The pseudocode has OTHERWISE OUTPUT "Invalid". How is this written in Python?
A otherwise:
B else:
5. Which is the correct way to write the end of a CASE OF Move as Python?
A else: followed by an indented print("Invalid")
B else Move: followed by an indented print("Invalid")

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1elif Move == "B": Aif Move == "F":
2else: BTests the next CASE value after the first, like the line B: in a CASE OF Move statement
3The first value in CASE OF Move becomes an if that tests the variable Move CThe second value uses if instead of elif, so it is a separate test and not part of the same chain. It should be elif Move == "B":
4Every value in the chain tests the same variable, so each elif compares Move again DOTHERWISE does not exist in Python. The catch-all branch must be written as else:
5if Move == "F": print("Forward") if Move == "B": print("Back") Eelif Move == "L":
6elif Move == "B": print("Back") OTHERWISE: print("Invalid") FRuns when none of the if or elif tests match, just like OTHERWISE in a CASE statement

Definitions

if/elif/else

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
__ move == "L": print("Left") elif move == "R":
2.
if move == "L": print("Left") ____ move == "R":
3.
elif move == "R": print("Right") ____:
4.
if move __ "L": print("Left") elif move == "R":
5.
if move == "L": print("Left") elif ____ == "R":

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A move = input("Move: ")
B if move = "L":
C print("Left")
D elif move == "R":
E print("Right")
Question 2
A move = input("Move: ")
B if move == "F"
C print("Forward")
D else:
E print("Invalid")
Question 3
A move = input("Move: ")
B elif move == "U":
C y = y + 1
D elif move == "D":
E y = y - 1
Question 4
A move = input("Move: ")
B if move == "U":
C print("Up")
D otherwise:
E print("Unknown move")
Question 5
A if move == "L":
B x = x - 1
C else if move == "R":
D x = x + 1

Answer Key

CASE as if/elif/else in Python
Section A: Quick Questions
Q1
A — An if/elif/else chain that tests Move each time
Q2
B — if Move == "L":
Q3
A — elif Move == "R":
Q4
B — else:
Q5
A — else: followed by an indented print("Invalid")
Section B: Match the Pairs
Q1
elif Move == "B": → Tests the next CASE value after the first, like the line B: in a CASE OF Move statement
Q2
else: → Runs when none of the if or elif tests match, just like OTHERWISE in a CASE statement
Q3
The first value in CASE OF Move becomes an if that tests the variable Move → if Move == "F":
Q4
Every value in the chain tests the same variable, so each elif compares Move again → elif Move == "L":
Q5
if Move == "F": print("Forward") if Move == "B": print("Back") → The second value uses if instead of elif, so it is a separate test and not part of the same chain. It should be elif Move == "B":
Q6
elif Move == "B": print("Back") OTHERWISE: print("Invalid") → OTHERWISE does not exist in Python. The catch-all branch must be written as else:
Section C: Definitions
Q1
if/elif/else — Python has no CASE, so write it as an if/elif chain testing the same variable. The first value uses if, each other value uses elif, and OTHERWISE becomes else.
Section D: Fill in the Blanks
Q1
if move == "L": print("Left") elif move == "R":
Q2
if move == "L": print("Left") elif move == "R":
Q3
elif move == "R": print("Right") else:
Q4
if move == "L": print("Left") elif move == "R":
Q5
if move == "L": print("Left") elif move == "R":
Section E: Spot the Error
Q1
B — A single = assigns a value. To test whether move equals "L" you need the comparison operator ==, so the line should be if move == "L":.
Q2
B — Every if line in Python must end with a colon. The line should be if move == "F":.
Q3
B — The first value of the CASE must use if, not elif. An elif cannot start a chain because there is no if before it. It should be if move == "U":.
Q4
D — Python has no OTHERWISE keyword. OTHERWISE in a CASE statement becomes else: in Python.
Q5
C — Python does not use else if. Each extra value in the CASE is written as elif, so the line should be elif move == "R":.