-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.py
530 lines (445 loc) · 16.3 KB
/
quiz.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import streamlit as st
import random
questions = [
{
"question": "Which of the following statements will create a list of squares from 1 to 10?",
"options": [
"[x * x for x in range(1, 11)]",
"[x ** 2 for x in range(0, 10)]",
"range(x * x, 10)",
"list(x**2 for x in range(1,10))"
],
"answer": 0
},
{
"question": "What will be the output of: print('a' * 3 + 'b' * 2)?",
"options": ["aaabb", "aaabbb", "ab", "a3b2"],
"answer": 0
},
{
"question": "What is the result of: [1, 2] + [3, 4]?",
"options": ["[1, 2, 3, 4]", "[4, 6]", "[1, 2, [3, 4]]", "Error"],
"answer": 0
},
{
"question": "Which method can convert a string to lowercase?",
"options": ["lowercase()", "strtolower()", "lower()", "tolower()"],
"answer": 2
},
{
"question": "What does this return: type([])?",
"options": ["<class 'list'>", "list", "array", "<list>"],
"answer": 0
},
{
"question": "What will the output be? bool([])",
"options": ["True", "False", "None", "Error"],
"answer": 1
},
{
"question": "What is the result of: 5 // 2?",
"options": ["2", "2.5", "3", "Error"],
"answer": 0
},
{
"question": "Which one is a valid Python set?",
"options": [
"[1, 2, 3]",
"{1, 2, 3}",
"(1, 2, 3)",
"<1, 2, 3>"
],
"answer": 1
},
{
"question": "How to get the number of key-value pairs in a dictionary?",
"options": ["dict.count()", "len(dict)", "dict.length()", "count(dict)"],
"answer": 1
},
{
"question": "What will be the output: 'Python'[0]?",
"options": ["P", "Y", "n", "Error"],
"answer": 0
},
{
"question": "Which loop will execute at least once even if the condition is false?",
"options": ["while", "for", "do-while (not in Python)", "None"],
"answer": 3
},
{
"question": "What is the result of this expression: not (True and False)?",
"options": ["True", "False", "None", "Error"],
"answer": 0
},
{
"question": "What does the 'pass' statement do?",
"options": ["Skips execution", "Ends program", "Does nothing", "Returns False"],
"answer": 2
},
{
"question": "Which of the following is NOT a valid Python data type?",
"options": ["list", "tuple", "dict", "record"],
"answer": 3
},
{
"question": "Which function returns a sequence of numbers in Python?",
"options": ["range()", "enumerate()", "list()", "seq()"],
"answer": 0
},
{
"question": "Which keyword is used to define an anonymous function?",
"options": ["lambda", "anonymous", "def", "function"],
"answer": 0
},
{
"question": "How to handle a division by zero exception?",
"options": [
"if divisor == 0 then print error",
"try: x/0 except ZeroDivisionError: pass",
"catch(ZeroDivisionError)",
"None of the above"
],
"answer": 1
},
{
"question": "What is the output of: [i for i in range(5) if i % 2 == 0]?",
"options": ["[1, 2, 3, 4]", "[0, 2, 4]", "[0, 1, 2, 3, 4]", "[2, 4]"],
"answer": 1
},
{
"question": "Which statement about tuples is true?",
"options": [
"They are mutable",
"They can contain duplicates",
"They cannot contain other tuples",
"They are slower than lists"
],
"answer": 1
},
{
"question": "Which built-in function returns the memory address of an object?",
"options": ["id()", "address()", "mem()", "ptr()"],
"answer": 0
},
{
"question": "Which is true about Python functions?",
"options": [
"They can return multiple values",
"They can be passed as arguments",
"They can be nested",
"All of the above"
],
"answer": 3
},
{
"question": "Which will raise a syntax error?",
"options": [
"x = 10\nif x > 5:\n print(x)",
"for i in range(5): print(i)",
"def func(x): return x * 2",
"x == 5"
],
"answer": 3
},
{
"question": "What is the output of: 3 < 4 and 5 > 6?",
"options": ["True", "False", "Error", "None"],
"answer": 1
},
{
"question": "Which operator is used to concatenate two strings?",
"options": ["+", "&", "*", "."],
"answer": 0
},
{
"question": "How do you get keys from a dictionary?",
"options": ["dict.keys()", "dict.values()", "dict.get()", "dict.items()"],
"answer": 0
},
{
"question": "What is a lambda function?",
"options": [
"A function without a name",
"A recursive function",
"A deprecated function",
"None of the above"
],
"answer": 0
},
{
"question": "Which function returns a sorted version of a list?",
"options": ["sort()", "sorted()", "arrange()", "order()"],
"answer": 1
},
{
"question": "Which one is an immutable data type?",
"options": ["list", "dict", "tuple", "set"],
"answer": 2
},
{
"question": "What happens if you call len() on a number?",
"options": ["Returns 1", "Raises TypeError", "Returns 0", "Returns length of digits"],
"answer": 1
},
{
"question": "What does the zip() function do?",
"options": [
"Combines multiple iterables element-wise",
"Compresses strings",
"Sorts data",
"None of the above"
],
"answer": 0
}
]
def main():
st.set_page_config(
page_title="Python Concepts Quiz",
page_icon="",
layout="wide",
initial_sidebar_state="collapsed"
)
st.markdown("""
<style>
.main {
padding: 0rem 1rem;
max-width: 100%;
}
.stRadio > div {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.stRadio label {
padding: 0.5rem;
border-radius: 0.3rem;
margin-bottom: 0.3rem;
transition: background-color 0.3s;
}
.subheading {
text-align: left;
font-weight: 600;
font-size: 20px;
}
@media (prefers-color-scheme: light) {
.stRadio label:hover {
background-color: #f7f7f7;
}
.subheading {
color: #4B5563;
}
.option-container {
border: 1px solid #e0e0e0;
background-color: #ffffff;
}
}
@media (prefers-color-scheme: dark) {
.stRadio label:hover {
background-color: #2d2d2d;
}
.subheading {
color: #e0e0e0;
}
.element-container .stTextInput input,
.element-container .stTextArea textarea,
.element-container .stNumberInput input,
.element-container .stDateInput input {
background-color: #262730;
color: #ffffff;
border-color: #4e4e4e;
}
.streamlit-expanderHeader {
background-color: #262730;
color: #ffffff;
}
.stAlert {
border-radius: 4px;
}
}
@media (max-width: 768px) {
.main {
padding: 0rem 0.5rem;
}
.stButton button {
width: 100%;
}
}
.option-container {
padding: 10px;
border-radius: 5px;
margin-bottom: 8px;
}
.stProgress .st-eb {
background-color: #4CAF50 !important;
}
</style>
""", unsafe_allow_html=True)
st.title("Python Concepts Quiz")
st.markdown("<div class='subheading'>from step 00_google_colab to step 09_exception_handling</div>", unsafe_allow_html=True)
st.markdown("---")
if 'quiz_started' not in st.session_state:
st.session_state.quiz_started = False
st.session_state.quiz_completed = False
st.session_state.current_question = 0
st.session_state.score = 0
st.session_state.answers = []
st.session_state.questions = []
st.session_state.selected_options = {}
if not st.session_state.quiz_started:
start_screen()
elif st.session_state.quiz_completed:
show_results()
else:
display_question()
def start_screen():
st.markdown("### Welcome to the Python Quiz!")
st.markdown("Test your Python knowledge with this interactive quiz.")
container = st.container()
with container:
num_questions = st.select_slider(
"Number of questions:",
options=[5, 10, 15, 20, 30],
value=10
)
st.markdown("### Ready?")
if st.button("Start Quiz", use_container_width=True, type="primary"):
if len(questions) < num_questions:
num_questions = len(questions)
st.session_state.questions = random.sample(questions, num_questions)
st.session_state.quiz_started = True
st.session_state.answers = [None] * num_questions
st.rerun()
st.markdown("</div>", unsafe_allow_html=True)
def display_question():
st.markdown("<div class='quiz-container'>", unsafe_allow_html=True)
question_data = st.session_state.questions[st.session_state.current_question]
total_questions = len(st.session_state.questions)
progress = (st.session_state.current_question) / total_questions
st.progress(progress)
st.markdown(f"### Question {st.session_state.current_question + 1} of {total_questions}")
st.markdown(f"**{question_data['question']}**")
q_key = f"q{st.session_state.current_question}"
if q_key not in st.session_state.selected_options:
st.session_state.selected_options[q_key] = st.session_state.answers[st.session_state.current_question]
index = None
if st.session_state.selected_options[q_key] is not None:
index = question_data["options"].index(st.session_state.selected_options[q_key])
selected_option = st.radio(
"Select your answer:",
question_data["options"],
key=q_key,
index=index,
label_visibility="collapsed",
on_change=option_selected,
args=(st.session_state.current_question, question_data["options"])
)
st.markdown("---")
screen_width = 800
if screen_width <= 768:
col1, col2 = st.columns(2)
with col1:
if st.session_state.current_question > 0:
if st.button("← Previous", use_container_width=True):
st.session_state.current_question -= 1
st.rerun()
with col2:
if st.session_state.current_question < total_questions - 1:
if st.button("Next →", use_container_width=True):
st.session_state.current_question += 1
st.rerun()
else:
if st.button("Submit", use_container_width=True, type="primary"):
calculate_score()
st.session_state.quiz_completed = True
st.rerun()
else:
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
if st.session_state.current_question > 0:
if st.button("← Previous", use_container_width=True):
st.session_state.current_question -= 1
st.rerun()
with col3:
if st.session_state.current_question < total_questions - 1:
if st.button("Next →", use_container_width=True):
st.session_state.current_question += 1
st.rerun()
else:
if st.button("Submit Quiz", use_container_width=True, type="primary"):
calculate_score()
st.session_state.quiz_completed = True
st.rerun()
st.markdown("</div>", unsafe_allow_html=True)
def option_selected(question_index, options):
q_key = f"q{question_index}"
selected_option = st.session_state[q_key]
st.session_state.answers[question_index] = selected_option
st.session_state.selected_options[q_key] = selected_option
if question_index < len(st.session_state.questions) - 1:
st.session_state.current_question = question_index + 1
st.rerun()
elif question_index == len(st.session_state.questions) - 1:
pass
def calculate_score():
score = 0
for i, question in enumerate(st.session_state.questions):
correct_answer = question["options"][question["answer"]]
if st.session_state.answers[i] == correct_answer:
score += 1
st.session_state.score = score
def show_results():
st.markdown("<div class='quiz-container'>", unsafe_allow_html=True)
total_questions = len(st.session_state.questions)
score_percentage = (st.session_state.score / total_questions) * 100
st.markdown("### Quiz Results")
screen_width = 800
if screen_width <= 768:
st.metric("Score", f"{st.session_state.score}/{total_questions}", f"{score_percentage:.1f}%")
if score_percentage >= 80:
st.success("Excellent job! You're a Python expert! 🏆")
elif score_percentage >= 60:
st.info("Good work! You know Python well! 👍")
else:
st.warning("Keep practicing to improve your Python skills! 💪")
st.markdown("### Performance")
results_chart = {
"Correct": st.session_state.score,
"Incorrect": total_questions - st.session_state.score
}
st.bar_chart(results_chart)
else:
col1, col2 = st.columns(2)
with col1:
st.metric("Score", f"{st.session_state.score}/{total_questions}", f"{score_percentage:.1f}%")
if score_percentage >= 80:
st.success("Excellent job! You're a Python expert! 🏆")
elif score_percentage >= 60:
st.info("Good work! You know Python well! 👍")
else:
st.warning("Keep practicing to improve your Python skills! 💪")
with col2:
st.markdown("### Performance")
results_chart = {
"Correct": st.session_state.score,
"Incorrect": total_questions - st.session_state.score
}
st.bar_chart(results_chart)
st.markdown("---")
st.markdown("### Detailed Review")
for i, question in enumerate(st.session_state.questions):
with st.expander(f"Question {i+1}: {question['question']}"):
user_answer = st.session_state.answers[i] or "No answer provided"
correct_answer = question["options"][question["answer"]]
if user_answer == correct_answer:
st.success(f"Your answer: {user_answer} ✓")
else:
st.error(f"Your answer: {user_answer} ✗")
st.info(f"Correct answer: {correct_answer}")
st.markdown("---")
if st.button("Take Another Quiz", type="primary", use_container_width=True):
for key in list(st.session_state.keys()):
del st.session_state[key]
st.rerun()
st.markdown("</div>", unsafe_allow_html=True)
if __name__ == "__main__":
main()