Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -68,29 +68,34 @@ counters["visitors"] += 1
|
|
| 68 |
save_counters(counters)
|
| 69 |
|
| 70 |
# -------------------------
|
| 71 |
-
#
|
| 72 |
# -------------------------
|
| 73 |
-
def check_answer_and_next(question_index, selected_option,
|
| 74 |
question = quiz_questions[question_index]
|
| 75 |
explanation = ""
|
| 76 |
-
score = current_score
|
| 77 |
|
| 78 |
if selected_option == question["answer"]:
|
| 79 |
score += 1
|
| 80 |
-
explanation = f"β
Correct!
|
| 81 |
else:
|
| 82 |
-
explanation = f"β Incorrect.
|
| 83 |
|
| 84 |
next_index = question_index + 1
|
| 85 |
if next_index >= len(quiz_questions):
|
| 86 |
-
# Quiz
|
| 87 |
counters = load_counters()
|
| 88 |
counters["completions"] += 1
|
| 89 |
save_counters(counters)
|
| 90 |
-
return ("π Quiz Completed!",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
next_question = quiz_questions[next_index]
|
| 93 |
-
return (f"### Question {next_index+1}: {next_question['question']}",
|
| 94 |
gr.update(choices=next_question["options"], value=None),
|
| 95 |
explanation,
|
| 96 |
score,
|
|
@@ -102,9 +107,9 @@ def show_hint(question_index):
|
|
| 102 |
return f"π‘ Hint: {quiz_questions[question_index]['hint']}"
|
| 103 |
|
| 104 |
# -------------------------
|
| 105 |
-
# Build
|
| 106 |
# -------------------------
|
| 107 |
-
def
|
| 108 |
with gr.Blocks() as block:
|
| 109 |
question_md = gr.Markdown(f"### Question {i+1}: {q['question']}")
|
| 110 |
options_radio = gr.Radio(q["options"], label="Select your answer")
|
|
@@ -129,7 +134,7 @@ def create_question_block_with_auto_next(i, q):
|
|
| 129 |
# -------------------------
|
| 130 |
# Create quiz interface
|
| 131 |
# -------------------------
|
| 132 |
-
question_blocks = [
|
| 133 |
quiz_interface = gr.TabbedInterface(question_blocks, ["Q1", "Q2", "Q3", "Q4", "Q5"])
|
| 134 |
|
| 135 |
# Launch
|
|
|
|
| 68 |
save_counters(counters)
|
| 69 |
|
| 70 |
# -------------------------
|
| 71 |
+
# Quiz functions
|
| 72 |
# -------------------------
|
| 73 |
+
def check_answer_and_next(question_index, selected_option, score):
|
| 74 |
question = quiz_questions[question_index]
|
| 75 |
explanation = ""
|
|
|
|
| 76 |
|
| 77 |
if selected_option == question["answer"]:
|
| 78 |
score += 1
|
| 79 |
+
explanation = f"β
Correct!\n\n{question['explanation']}"
|
| 80 |
else:
|
| 81 |
+
explanation = f"β Incorrect.\n\n{question['explanation']}"
|
| 82 |
|
| 83 |
next_index = question_index + 1
|
| 84 |
if next_index >= len(quiz_questions):
|
| 85 |
+
# Quiz completed β update completions
|
| 86 |
counters = load_counters()
|
| 87 |
counters["completions"] += 1
|
| 88 |
save_counters(counters)
|
| 89 |
+
return ("π Quiz Completed!",
|
| 90 |
+
"",
|
| 91 |
+
explanation + f"\n\n**Your Score: {score}/{len(quiz_questions)}**",
|
| 92 |
+
score,
|
| 93 |
+
next_index,
|
| 94 |
+
counters["visitors"],
|
| 95 |
+
counters["completions"])
|
| 96 |
else:
|
| 97 |
next_question = quiz_questions[next_index]
|
| 98 |
+
return (f"### Question {next_index+1}: {next_question['question']}",
|
| 99 |
gr.update(choices=next_question["options"], value=None),
|
| 100 |
explanation,
|
| 101 |
score,
|
|
|
|
| 107 |
return f"π‘ Hint: {quiz_questions[question_index]['hint']}"
|
| 108 |
|
| 109 |
# -------------------------
|
| 110 |
+
# Build interface
|
| 111 |
# -------------------------
|
| 112 |
+
def create_question_block(i, q):
|
| 113 |
with gr.Blocks() as block:
|
| 114 |
question_md = gr.Markdown(f"### Question {i+1}: {q['question']}")
|
| 115 |
options_radio = gr.Radio(q["options"], label="Select your answer")
|
|
|
|
| 134 |
# -------------------------
|
| 135 |
# Create quiz interface
|
| 136 |
# -------------------------
|
| 137 |
+
question_blocks = [create_question_block(i, q) for i, q in enumerate(quiz_questions)]
|
| 138 |
quiz_interface = gr.TabbedInterface(question_blocks, ["Q1", "Q2", "Q3", "Q4", "Q5"])
|
| 139 |
|
| 140 |
# Launch
|