▍程式碼
# views.py
@app.route('/submit_quiz', methods=['POST'])
def submit_quiz():
...
WJSON_PATH = os.path.join( "錯題資訊存檔路徑" )
# 更新錯誤次數
if mistakes_to_log:
if not os.path.exists(WJSON_PATH):
print(f"錯誤:找不到檔案 {WJSON_PATH},無法更新錯誤次數。")
else:
try:
# 讀檔
with open(WJSON_PATH, 'r', encoding='utf-8') as f:
data_from_file = json.load(f)
if not isinstance(data_from_file, list):
print("錯誤:JSON 檔案結構不是列表,無法更新。")
data_map = {}
# 以題目作為key
for item in data_from_file:
unique_key = item.get('題目', '')
data_map[unique_key] = item
updated_count = 0
for mistake in mistakes_to_log:
unique_key = mistake.get('題目', '')
if unique_key in data_map:
question_record = data_map[unique_key]
current_count = question_record.get('error_count', 0)
question_record['error_count'] = current_count + 1
updated_count += 1
else:
print(f"警告:找不到錯題 '{mistake.get('題目', '')}' 對應的記錄。")
# 更新json檔
with open(WJSON_PATH, 'w', encoding='utf-8') as f:
json.dump(data_from_file, f, ensure_ascii=False, indent=4)
print(f"成功更新。")
except Exception as e:
print(f"更新json檔時發生錯誤: {e}")
final_result = {
...
}
...