Spaces:
Runtime error
Runtime error
Create self-learning-quiz.py
Browse files- self-learning-quiz.py +39 -0
self-learning-quiz.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
questions = [
|
4 |
+
{"question": "Who is the narrator in 'The Road Not Taken'?", "options": ["A young boy", "An old man", "A traveler", "None of the above"], "correct_option": 2},
|
5 |
+
{"question": "What is the setting of the poem?", "options": ["A city", "A forest", "A beach", "A mountain"], "correct_option": 1},
|
6 |
+
{"question": "What is the underlying message of the poem?", "options": ["The importance of making choices", "The beauty of nature", "The joy of youth", "The value of time"], "correct_option": 0}
|
7 |
+
]
|
8 |
+
|
9 |
+
def interactive_quiz(user_response=None, continue_quiz='yes', question_index=0):
|
10 |
+
output_messages = []
|
11 |
+
|
12 |
+
current_question = questions[question_index]
|
13 |
+
output_messages.append(f"Question {question_index + 1}: {current_question['question']}")
|
14 |
+
for i, option in enumerate(current_question['options']):
|
15 |
+
output_messages.append(f"{i}. {option}")
|
16 |
+
|
17 |
+
if user_response is not None:
|
18 |
+
user_response = int(user_response)
|
19 |
+
if user_response == current_question['correct_option']:
|
20 |
+
output_messages.append("Correct!")
|
21 |
+
else:
|
22 |
+
output_messages.append(f"Incorrect. The correct answer was: {current_question['options'][current_question['correct_option']]}")
|
23 |
+
|
24 |
+
if continue_quiz.lower() == 'no':
|
25 |
+
output_messages.append("Thank you for participating in the quiz!")
|
26 |
+
question_index = 0
|
27 |
+
else:
|
28 |
+
question_index = (question_index + 1) % len(questions)
|
29 |
+
|
30 |
+
return "\n".join(output_messages), question_index
|
31 |
+
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=interactive_quiz,
|
34 |
+
inputs=["text", "text", "number"],
|
35 |
+
outputs=["text", "number"],
|
36 |
+
live=True
|
37 |
+
)
|
38 |
+
|
39 |
+
iface.launch()
|