-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatch-case-explained-quiz.py
More file actions
181 lines (167 loc) · 6.21 KB
/
match-case-explained-quiz.py
File metadata and controls
181 lines (167 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Quiz: Match/Case Explained
Review: concepts/match-case-explained.md
"""
from _quiz_helpers import normalize_answer
def run_quiz():
print("=" * 60)
print(" QUIZ: Match/Case Explained")
print(" Review: concepts/match-case-explained.md")
print("=" * 60)
print()
score = 0
total = 7
# Question 1 — basic understanding
print("Question 1/7: What Python version introduced match/case?")
print()
print(" a) Python 3.8")
print(" b) Python 3.9")
print(" c) Python 3.10")
print(" d) Python 3.12")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! Structural pattern matching was introduced")
print("in Python 3.10 (PEP 634).")
else:
print("Incorrect. The answer is c) Python 3.10.")
print("match/case is available from Python 3.10 onward.")
print()
# Question 2 — wildcard pattern
print("Question 2/7: What does case _: do in a match statement?")
print()
print(" a) Matches only the underscore character")
print(" b) Matches anything (like 'else' in if/elif)")
print(" c) Raises an error")
print(" d) Skips the current iteration")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! The underscore _ is a wildcard pattern.")
print("It matches anything, like a catch-all 'else' branch.")
else:
print("Incorrect. The answer is b).")
print("case _: is the wildcard — it matches any value,")
print("similar to the 'else' clause in if/elif/else.")
print()
# Question 3 — OR patterns
print("Question 3/7: How do you match multiple values in one case?")
print()
print(" a) case 'a' and 'b':")
print(" b) case 'a', 'b':")
print(" c) case 'a' | 'b':")
print(" d) case 'a' or 'b':")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! The pipe | operator combines patterns.")
print("case 'a' | 'b': matches either 'a' or 'b'.")
else:
print("Incorrect. The answer is c) case 'a' | 'b':")
print("Use the | operator to create OR patterns.")
print()
# Question 4 — capture pattern
print("Question 4/7: What does x capture in this pattern?")
print()
print(" match point:")
print(" case (x, 0):")
print(" print(f'On x-axis at {x}')")
print()
print(" a) The literal character 'x'")
print(" b) The first element of the tuple")
print(" c) The entire tuple")
print(" d) Nothing — x must be defined beforehand")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! x captures the first element of the tuple.")
print("The pattern matches any tuple where the second element is 0.")
else:
print("Incorrect. The answer is b).")
print("Bare names in patterns are capture variables. x gets")
print("the value of the first tuple element.")
print()
# Question 5 — guard clause
print("Question 5/7: What is the guard clause in this pattern?")
print()
print(" match age:")
print(" case n if n < 0:")
print(" print('Invalid')")
print()
print(" a) case n")
print(" b) if n < 0")
print(" c) print('Invalid')")
print(" d) match age")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! The 'if n < 0' part is the guard clause.")
print("It adds an extra condition to the pattern match.")
else:
print("Incorrect. The answer is b) if n < 0.")
print("A guard clause adds a condition after the pattern.")
print("The case only matches if both the pattern and guard are true.")
print()
# Question 6 — variable name trap
print("Question 6/7: What is wrong with this code?")
print()
print(" STATUS_OK = 200")
print()
print(" match code:")
print(" case STATUS_OK:")
print(" print('OK')")
print()
print(" a) Nothing — it correctly matches code == 200")
print(" b) STATUS_OK becomes a capture variable, not a comparison")
print(" c) Constants cannot be used in match statements")
print(" d) case needs parentheses around STATUS_OK")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! Bare names in patterns are capture variables.")
print("STATUS_OK captures the value of code instead of comparing.")
print("Use a literal (case 200:) or dotted name instead.")
else:
print("Incorrect. The answer is b).")
print("Bare names are ALWAYS capture variables in patterns.")
print("To match a constant, use the literal value: case 200:")
print()
# Question 7 — when to use
print("Question 7/7: When is match/case most useful?")
print()
print(" a) For all conditional logic — always use it instead of if/elif")
print(" b) For matching the structure and shape of data")
print(" c) Only for matching strings")
print(" d) Only when you have exactly two possible cases")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! match/case excels at structural pattern matching —")
print("matching the shape of tuples, dicts, and class instances.")
else:
print("Incorrect. The answer is b).")
print("match/case is designed for structural matching. For simple")
print("value comparisons, if/elif is often clearer.")
print()
# Final score
print("=" * 60)
pct = round(score / total * 100)
print(f" Final Score: {score}/{total} ({pct}%)")
print()
if pct == 100:
print(" Perfect score! You understand match/case well.")
elif pct >= 70:
print(" Good work! Review any questions you missed.")
else:
print(" Keep practicing! Re-read concepts/match-case-explained.md")
print(" and try again.")
print("=" * 60)
if __name__ == "__main__":
run_quiz()