dayuian commited on
Commit
6247532
·
verified ·
1 Parent(s): e10d847

Update quiz.py

Browse files
Files changed (1) hide show
  1. quiz.py +22 -3
quiz.py CHANGED
@@ -3,7 +3,6 @@ from vocab import get_words_from_source
3
  from sentences import get_sentence
4
 
5
  # 生成單字選擇題
6
-
7
  def generate_fill_in_blank_exam(source, num):
8
  words_data = get_words_from_source(source)
9
  words = random.sample(words_data, num)
@@ -34,7 +33,18 @@ def generate_fill_in_blank_exam(source, num):
34
  "phonetic": phonetic
35
  })
36
 
37
- return questions
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  # 自動對答案並計分
@@ -55,4 +65,13 @@ def check_exam(user_answers, questions):
55
  correct_count += 1
56
 
57
  score = f"{correct_count}/{len(questions)} 分"
58
- return score, results
 
 
 
 
 
 
 
 
 
 
3
  from sentences import get_sentence
4
 
5
  # 生成單字選擇題
 
6
  def generate_fill_in_blank_exam(source, num):
7
  words_data = get_words_from_source(source)
8
  words = random.sample(words_data, num)
 
33
  "phonetic": phonetic
34
  })
35
 
36
+ # 將問題渲染為 HTML 以便 Gradio 顯示
37
+ if not questions:
38
+ return "<p style='color:red;'>❌ 無法生成試卷,可能單字缺少例句</p>"
39
+
40
+ exam_html = ""
41
+ for i, q in enumerate(questions):
42
+ exam_html += f"<p><strong>第 {i + 1} 題:</strong> {q['sentence']}</p>"
43
+ for option in q['options']:
44
+ exam_html += f"<input type='radio' name='q{i}' value='{option}'> {option} "
45
+ exam_html += "<br>"
46
+
47
+ return exam_html
48
 
49
 
50
  # 自動對答案並計分
 
65
  correct_count += 1
66
 
67
  score = f"{correct_count}/{len(questions)} 分"
68
+
69
+ # 顯示結果
70
+ result_html = f"<p><strong>得分:</strong> {score}</p>"
71
+ for res in results:
72
+ color = 'green' if res['is_correct'] else 'red'
73
+ result_html += f"<p style='color:{color};'><strong>題目:</strong> {res['question']}<br>" \
74
+ f"<strong>你的答案:</strong> {res['user_answer']}<br>" \
75
+ f"<strong>正確答案:</strong> {res['correct_answer']}</p>"
76
+
77
+ return result_html