-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhat-is-a-variable-quiz.py
More file actions
191 lines (175 loc) · 6.21 KB
/
what-is-a-variable-quiz.py
File metadata and controls
191 lines (175 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
182
183
184
185
186
187
188
189
190
191
"""
Quiz: What is a Variable?
Review: concepts/what-is-a-variable.md
"""
from _quiz_helpers import normalize_answer, ask_true_false, ask_code_completion
def run_quiz():
print("=" * 60)
print(" QUIZ: What is a Variable?")
print(" Review: concepts/what-is-a-variable.md")
print("=" * 60)
print()
score = 0
total = 9
# Question 1 — multiple choice
print("Question 1/9: Which of these is a valid Python variable name?")
print()
print(" a) 2nd_place")
print(" b) second-place")
print(" c) second_place")
print(" d) second place")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! Variable names can contain letters, numbers, and")
print("underscores, but must start with a letter or underscore.")
else:
print("Incorrect. The answer is c) second_place.")
print("Names must start with a letter or underscore. No hyphens or spaces.")
print()
# Question 2 — code prediction
print("Question 2/9: What will this code print?")
print()
print(' name = "Alice"')
print(' name = "Bob"')
print(" print(name)")
print()
print(" a) Alice")
print(" b) Bob")
print(" c) AliceBob")
print(" d) Error")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! Assigning a new value replaces the old one.")
print("The variable now holds 'Bob'.")
else:
print("Incorrect. The answer is b) Bob.")
print("The second assignment replaces 'Alice' with 'Bob'.")
print()
# Question 3 — understanding = vs ==
print("Question 3/9: What is the difference between = and == in Python?")
print()
print(" a) They do the same thing")
print(" b) = assigns a value, == checks equality")
print(" c) = checks equality, == assigns a value")
print(" d) == is used for strings, = is used for numbers")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! x = 5 stores 5 in x. x == 5 checks if x equals 5.")
else:
print("Incorrect. The answer is b).")
print("= is assignment (store a value). == is comparison (check equality).")
print()
# Question 4 — case sensitivity
print("Question 4/9: Are 'Score' and 'score' the same variable in Python?")
print()
print(" a) Yes, Python ignores case")
print(" b) No, Python is case-sensitive")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! Python is case-sensitive. Score and score are")
print("two completely different variables.")
else:
print("Incorrect. The answer is b).")
print("Python treats uppercase and lowercase letters as different.")
print()
# Question 5 — code prediction
print("Question 5/9: What happens when you run this code?")
print()
print(" print(score)")
print(" score = 100")
print()
print(" a) Prints 100")
print(" b) Prints None")
print(" c) NameError — score is not defined yet")
print(" d) Prints 0")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! You cannot use a variable before creating it.")
print("Python reads top to bottom, so score does not exist at line 1.")
else:
print("Incorrect. The answer is c) NameError.")
print("The variable must be created before it is used.")
print()
# Question 6 — naming convention
print("Question 6/9: What naming convention does Python recommend")
print("for variables?")
print()
print(" a) camelCase (studentCount)")
print(" b) PascalCase (StudentCount)")
print(" c) snake_case (student_count)")
print(" d) UPPER_CASE (STUDENT_COUNT)")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! Python convention is snake_case for variables:")
print("lowercase_with_underscores.")
else:
print("Incorrect. The answer is c) snake_case.")
print("Python uses lowercase_with_underscores for variable names.")
print()
# Question 7 — short answer
print("Question 7/9: What will this code print?")
print()
print(" age = 25")
print(" next_year = age + 1")
print(" print(next_year)")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "26":
score += 1
print("Correct! age holds 25, so age + 1 is 26, which gets")
print("stored in next_year.")
else:
print("Incorrect. The answer is 26.")
print("age is 25, and 25 + 1 = 26.")
print()
# Question 8 — true/false
if ask_true_false(
question_num=8,
total=total,
statement="In Python, you must declare a variable's type before using it.",
correct=False,
explanation_correct="Python figures out the type automatically based on the value you assign.",
explanation_incorrect="Python is dynamically typed. You just write x = 5, and Python knows it is an integer.",
):
score += 1
# Question 9 — code completion
if ask_code_completion(
question_num=9,
total=total,
prompt="Assign the value 42 to a variable called answer:",
code_lines=[
"____ = 42",
],
correct_answers=["answer"],
explanation_correct="Variable names go on the left side of =.",
explanation_incorrect="The variable name goes on the left side of the = sign.",
case_sensitive=True,
):
score += 1
# Final score
print("=" * 60)
pct = round(score / total * 100)
print(f" Final Score: {score}/{total} ({pct}%)")
print()
if pct == 100:
print(" Perfect score! You have a solid grasp of variables.")
elif pct >= 70:
print(" Good work! Review any questions you missed.")
else:
print(" Keep practicing! Re-read concepts/what-is-a-variable.md")
print(" and try again.")
print("=" * 60)
if __name__ == "__main__":
run_quiz()