Arvind-Science123 commited on
Commit
a0abdb1
Β·
verified Β·
1 Parent(s): beaa562

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -68,29 +68,34 @@ counters["visitors"] += 1
68
  save_counters(counters)
69
 
70
  # -------------------------
71
- # Functions
72
  # -------------------------
73
- def check_answer_and_next(question_index, selected_option, current_score):
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! {question['explanation']}"
81
  else:
82
- explanation = f"❌ Incorrect. {question['explanation']}"
83
 
84
  next_index = question_index + 1
85
  if next_index >= len(quiz_questions):
86
- # Quiz finished β†’ increment completion
87
  counters = load_counters()
88
  counters["completions"] += 1
89
  save_counters(counters)
90
- return ("πŸŽ‰ Quiz Completed!", "", explanation + f"\n\nYour Score: {score}/{len(quiz_questions)}", score, next_index, counters["visitors"], counters["completions"])
 
 
 
 
 
 
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 UI
106
  # -------------------------
107
- def create_question_block_with_auto_next(i, q):
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 = [create_question_block_with_auto_next(i, q) for i, q in enumerate(quiz_questions)]
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