其他分享
首页 > 其他分享> > 如何建立自己的题库,实现往题库中输入问题、答案,且让题库随机出题

如何建立自己的题库,实现往题库中输入问题、答案,且让题库随机出题

作者:互联网

原理:

本人把题库用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
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)
            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)
            creationS.appendChild(creationA)
            chapter.appendChild(creationS)
            with open(f'{xmlName}.xml', 'w', encoding='utf-8') as f:
                DomTree.writexml(f, addindent='', encoding='utf-8')
            write_question()


    write_question()

标签:xml,segment,question,出题,随机,print,题库,type
来源: https://www.cnblogs.com/daxiangcai/p/16126844.html