rameshmoorthy commited on
Commit
f35100d
·
verified ·
1 Parent(s): 1b5109f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -11
app.py CHANGED
@@ -95,21 +95,46 @@ def update_quiz_components(quiz_data):
95
  radio_updates.append(radio_update)
96
  return radio_updates + [gr.update(value="Please select answers and click 'Check Score'.", visible=True)]
97
 
98
- def collect_answers_and_calculate(*radio_values, /, quiz_data): # / enforces quiz_data as positional-only after *radio_values
 
 
 
 
 
 
 
99
  print(f"Received radio_values: {radio_values}") # Debug print
100
  print(f"Received quiz_data: {quiz_data}") # Debug print
101
- user_answer_list = [answer for answer in radio_values if answer is not None]
 
 
102
  correct_answers = [item.correct_answer for item in quiz_data.items[:10]]
 
103
  print(f"User answers: {user_answer_list}") # Debug print
104
  print(f"Correct answers: {correct_answers}") # Debug print
105
- score = sum(1 for u, c in zip(user_answer_list, correct_answers) if u == c)
106
- print(f"Calculated score: {score}") # Debug print
107
- if score > 7:
108
- message = f"### Excellent! You got {score} out of 10!"
109
- elif score > 5:
110
- message = f"### Good! You got {score} out of 10!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  else:
112
- message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
 
113
  return message
114
 
115
  # Define a colorful theme
@@ -155,10 +180,10 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
155
  outputs=question_radios + [quiz_msg]
156
  )
157
 
158
- # Register the click event for Check Score with explicit unpacking
159
  check_score_btn.click(
160
  fn=collect_answers_and_calculate,
161
- inputs=question_radios + [quiz_data_state], # 10 radio values + 1 quiz_data
162
  outputs=[score_output],
163
  api_name="check_score"
164
  )
 
95
  radio_updates.append(radio_update)
96
  return radio_updates + [gr.update(value="Please select answers and click 'Check Score'.", visible=True)]
97
 
98
+ # FIXED FUNCTION: Changed parameter signature to accept all arguments positionally
99
+ def collect_answers_and_calculate(*all_inputs):
100
+ print(f"Total inputs received: {len(all_inputs)}") # Debug print
101
+
102
+ # The last input is quiz_data, the first 10 are radio values
103
+ radio_values = all_inputs[:10] # First 10 inputs are radio button values
104
+ quiz_data = all_inputs[10] # Last input is quiz_data
105
+
106
  print(f"Received radio_values: {radio_values}") # Debug print
107
  print(f"Received quiz_data: {quiz_data}") # Debug print
108
+
109
+ # Filter out None values but keep track of positions
110
+ user_answer_list = list(radio_values)
111
  correct_answers = [item.correct_answer for item in quiz_data.items[:10]]
112
+
113
  print(f"User answers: {user_answer_list}") # Debug print
114
  print(f"Correct answers: {correct_answers}") # Debug print
115
+
116
+ # Calculate score - only count answered questions
117
+ score = 0
118
+ answered_questions = 0
119
+ for u, c in zip(user_answer_list, correct_answers):
120
+ if u is not None: # Only count if user answered
121
+ answered_questions += 1
122
+ if u == c:
123
+ score += 1
124
+
125
+ print(f"Calculated score: {score}/{answered_questions}") # Debug print
126
+
127
+ if answered_questions == 0:
128
+ message = "### Please answer at least one question!"
129
+ elif score == answered_questions:
130
+ message = f"### Perfect! You got {score} out of {answered_questions} correct!"
131
+ elif score > answered_questions * 0.7:
132
+ message = f"### Excellent! You got {score} out of {answered_questions} correct!"
133
+ elif score > answered_questions * 0.5:
134
+ message = f"### Good! You got {score} out of {answered_questions} correct!"
135
  else:
136
+ message = f"### You got {score} out of {answered_questions} correct! Don't worry. You can prepare well and try better next time!"
137
+
138
  return message
139
 
140
  # Define a colorful theme
 
180
  outputs=question_radios + [quiz_msg]
181
  )
182
 
183
+ # FIXED: Register the click event for Check Score with correct input handling
184
  check_score_btn.click(
185
  fn=collect_answers_and_calculate,
186
+ inputs=question_radios + [quiz_data_state], # This creates a list of 11 inputs
187
  outputs=[score_output],
188
  api_name="check_score"
189
  )