如何建立自己的題庫,實現往題庫中輸入問題、答案,且讓題庫隨機出題


原理:

本人把題庫用xml文件的方式存儲,實際上就是xml的讀取和寫入的實現

示例題庫:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <chapter id="1" name="網球">
        <segment id="1">
            <question>網球拍</question>
            <answer>tennis racket</answer>
        </segment>
        <segment id="2">
            <question>共享單車</question>
            <answer>shared bike</answer>
        </segment>
    </chapter>
    <chapter id="2" name="歷史文化">
        <segment id="1">
            <question>講究/強調</question>
            <answer>attach great significance to</answer>
        </segment>
        <segment id="2">
            <question>以……為基礎</question>
            <answer>be set in</answer>
        </segment>
    </chapter>
</root>

首先導庫:

from xml.dom.minidom import parse  # 用於解析xml文件
import numpy  # 用於打亂列表順序

由於可能存在多個題庫,比如數學、英語題庫,故讓用戶輸入題庫名選擇題庫(即xml文件):

print('choose xml file:')
xmlName = input('type in:')

解析xml文件並獲取根元素:

DomTree = parse(f'{xmlName}.xml')
root = DomTree.documentElement

接下來讓用戶選擇做題還是往題庫中寫題:

print('for examination, type in 0\nfor creation, type in 1')
judge = input('type in:')

一個題庫中可能有多個章節,接下來展示章節名,讓用戶選擇章節(本人把章節名放在<chapter>標簽的name屬性中,見文章開頭):

print('chapter:')
chapterList = root.getElementsByTagName('chapter') # 根據標簽名獲取元素 for i in range(chapterList.length): print(chapterList[i].getAttribute('id') + '、' + chapterList[i].getAttribute('name')) # 獲取屬性值 chapterNumStr = input('choose chapter:') chapterNum = int(chapterNumStr)

獲取對應章節的問題和答案(均放在<segment>標簽下):

chapter = root.getElementsByTagName('chapter')[chapterNum - 1]
segment = chapter.getElementsByTagName('segment')

如若用戶選擇做題,詢問用戶按照順序做題還是打亂順序(numpy.random.permutation()方法)做題,並展示題目,用戶輸入答案后按下回車會展示參考答案並展示下一個題目,本人沒有判斷用戶輸入的答案是否正確,各位可以試着添加:

if judge == '0':
    print('out of order, type in 0\nin order, type in 1') ifRandom = input('type in:') if ifRandom == '0': for i in numpy.random.permutation(range(segment.length)): print('question:\n' + segment[i].getElementsByTagName('question')[0].childNodes[0].data) # 用於提取標簽內的文本,文本也算作一個節點,即childNodes[0]
       # 注意segment[i].childNodes[k].childNodes[0]也可以,但是這個方法在k>=2就無法使用了 print("answer:") pause = input('yours:') print("reference:" + segment[i].getElementsByTagName('answer')[0].childNodes[0].data + '\n') elif ifRandom == '1': for i in range(segment.length): print('question:\n' + segment[i].getElementsByTagName('question')[0].childNodes[0].data) print("answer:") pause = input('yours:') print("reference:" + segment[i].getElementsByTagName('answer')[0].childNodes[0].data + '\n')

如若用戶選擇往題庫中寫題,我們定義一個函數,讓用戶依次輸入問題、答案,並循環往復,用戶可以在輸入問題時輸入"break"來跳出循環,最后把問題節點(<question>)和答案節點(<answer>)懸掛到新生成的<segment>節點下,寫入xml就可以了:

segLength = segment.length
elif judge == '1': print('type in "break" in question to jump out of the loop') def write_question(): global segLength # 在函數中使用全局變量要先聲明 segLength += 1 q = input('type in question:') if q == 'break': print('*******************') print('Program terminated.') print('*******************') else: a = input('type in answer:') creationS = DomTree.createElement('segment') # 創造元素 creationS.setAttribute('id', str(segLength)) # 設置屬性值 creationQ = DomTree.createElement('question') textQ = DomTree.createTextNode(q) # 創造文本節點 creationQ.appendChild(textQ) # 把文本節點懸掛到標簽下 creationA = DomTree.createElement('answer') textA = DomTree.createTextNode(a) creationA.appendChild(textA) creationS.appendChild(creationQ) # 把<question>懸掛到<segment>下面 creationS.appendChild(creationA) chapter.appendChild(creationS) with open(f'{xmlName}.xml', 'w', encoding='utf-8') as f: DomTree.writexml(f, addindent='', encoding='utf-8') # 把DomTree重新寫入,實現更新 write_question() write_question()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM