LearnFrenzy
Sign in Take a free test

Conditional Statements · Predict the Output · Question 6

match-case with sequence patterns Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Python program?

main.py
def describe(cmd):
    match cmd.split():
        case ["go", direction]:
            return f"moving {direction}"
        case ["pick", "up", item]:
            return f"picking {item}"
        case ["quit" | "exit"]:
            return "bye"
        case _:
            return "unknown"

for c in ["go north", "pick up lamp", "exit", "go north fast"]:
    print(describe(c))
  • A moving north picking lamp bye moving north
  • B moving north unknown bye unknown
  • C moving north picking up bye unknown
  • D moving north picking lamp bye unknown Correct
Correct Answer: Option D

["go", direction] matches only a two-word list, so "go north fast" (three words) falls through to case _.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links