DigitalTransG2 commited on
Commit
678c9df
·
verified ·
1 Parent(s): 3b3de14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -60,6 +60,7 @@ QUESTIONS = [
60
  ]
61
 
62
  QUIZ_DURATION_SECONDS = 60 * 60 # 1 hour
 
63
 
64
  # --- Helper functions ---
65
 
@@ -82,11 +83,12 @@ def prepare_quiz():
82
  quiz.append({"q": q["q"], "options": opts, "answer": new_answer_index})
83
 
84
  st.session_state.quiz = quiz
85
- # initialize user answers (0 means not answered because we will add a placeholder at index 0 in selects)
 
86
  for i in range(len(quiz)):
87
  key = f"q{i}"
88
  if key not in st.session_state:
89
- st.session_state[key] = 0
90
 
91
 
92
  def compute_results():
@@ -97,9 +99,9 @@ def compute_results():
97
  total = len(st.session_state.quiz)
98
  score = 0
99
  for i, q in enumerate(st.session_state.quiz):
100
- sel_index = st.session_state.get(f"q{i}", 0)
101
- # our select index 0 means "-- Select --"; actual options start at 1
102
- if sel_index and (sel_index - 1) == q["answer"]:
103
  score += 1
104
 
105
  now = time.time()
@@ -142,8 +144,8 @@ with st.sidebar:
142
  st.write(f"Time used: {format_seconds(int(st.session_state.used_time))}")
143
 
144
  if st.button("Reset quiz"):
 
145
  for k in list(st.session_state.keys()):
146
- # keep page config but clear quiz-related keys
147
  if k.startswith("q") or k in ["quiz", "submitted", "score", "used_time", "start_time"]:
148
  del st.session_state[k]
149
  st.experimental_rerun()
@@ -157,9 +159,15 @@ if remaining == 0 and not st.session_state.submitted:
157
  st.markdown("---")
158
  for i, q in enumerate(st.session_state.quiz):
159
  st.write(f"**Question {i+1}.** {q['q']}")
160
- display_options = ["-- Select an option --"] + q["options"]
161
- # selectbox will store index (0..n)
162
- sel = st.selectbox("", display_options, index=st.session_state.get(f"q{i}", 0), key=f"q{i}")
 
 
 
 
 
 
163
  st.write("")
164
 
165
  st.markdown("---")
@@ -175,10 +183,10 @@ else:
175
  # Optionally show which answers were correct
176
  with st.expander("Review answers"):
177
  for i, q in enumerate(st.session_state.quiz):
178
- user_sel = st.session_state.get(f"q{i}", 0)
179
- user_text = "No answer" if user_sel == 0 else q["options"][user_sel - 1]
180
  correct_text = q["options"][q["answer"]]
181
- ok = "✅" if (user_sel and (user_sel - 1) == q["answer"]) else "❌"
182
  st.write(f"**Q{i+1}.** {q['q']}")
183
  st.write(f"Your answer: {user_text} {ok}")
184
  st.write(f"Correct answer: {correct_text}")
 
60
  ]
61
 
62
  QUIZ_DURATION_SECONDS = 60 * 60 # 1 hour
63
+ DEFAULT_PROMPT = "-- Select an option --"
64
 
65
  # --- Helper functions ---
66
 
 
83
  quiz.append({"q": q["q"], "options": opts, "answer": new_answer_index})
84
 
85
  st.session_state.quiz = quiz
86
+
87
+ # initialize user answers as the placeholder string so selectbox sees a valid value
88
  for i in range(len(quiz)):
89
  key = f"q{i}"
90
  if key not in st.session_state:
91
+ st.session_state[key] = DEFAULT_PROMPT
92
 
93
 
94
  def compute_results():
 
99
  total = len(st.session_state.quiz)
100
  score = 0
101
  for i, q in enumerate(st.session_state.quiz):
102
+ sel_text = st.session_state.get(f"q{i}", DEFAULT_PROMPT)
103
+ correct_text = q["options"][q["answer"]]
104
+ if sel_text != DEFAULT_PROMPT and sel_text == correct_text:
105
  score += 1
106
 
107
  now = time.time()
 
144
  st.write(f"Time used: {format_seconds(int(st.session_state.used_time))}")
145
 
146
  if st.button("Reset quiz"):
147
+ # clear quiz-related keys and restart
148
  for k in list(st.session_state.keys()):
 
149
  if k.startswith("q") or k in ["quiz", "submitted", "score", "used_time", "start_time"]:
150
  del st.session_state[k]
151
  st.experimental_rerun()
 
159
  st.markdown("---")
160
  for i, q in enumerate(st.session_state.quiz):
161
  st.write(f"**Question {i+1}.** {q['q']}")
162
+ display_options = [DEFAULT_PROMPT] + q["options"]
163
+ # use a non-empty label for accessibility but hide it visually
164
+ label = f"Answer for question {i+1}"
165
+ # ensure the session value is one of the options (prepare_quiz ensured this, but be defensive)
166
+ if st.session_state.get(f"q{i}") not in display_options:
167
+ st.session_state[f"q{i}"] = DEFAULT_PROMPT
168
+
169
+ # store the selected option string in session_state (not an index) to avoid serialization errors
170
+ sel = st.selectbox(label, display_options, key=f"q{i}", label_visibility="collapsed")
171
  st.write("")
172
 
173
  st.markdown("---")
 
183
  # Optionally show which answers were correct
184
  with st.expander("Review answers"):
185
  for i, q in enumerate(st.session_state.quiz):
186
+ user_sel = st.session_state.get(f"q{i}", DEFAULT_PROMPT)
187
+ user_text = "No answer" if user_sel == DEFAULT_PROMPT else user_sel
188
  correct_text = q["options"][q["answer"]]
189
+ ok = "✅" if (user_sel != DEFAULT_PROMPT and user_sel == correct_text) else "❌"
190
  st.write(f"**Q{i+1}.** {q['q']}")
191
  st.write(f"Your answer: {user_text} {ok}")
192
  st.write(f"Correct answer: {correct_text}")