最新消息:380元/半年,推荐全网最具性价比的一站式编程学习平台码丁实验室

Python自动化之试卷生成器

Python 少儿编程 3134浏览 0评论

友情提示:380元/半年,儿童学编程,就上码丁实验室

Python自动化之试卷生成器


我们要做什么

      我们想要生成多份试卷,试卷的内容是问中国的一些省的省会城市是哪个,形式为选择题,例子如下:

1.湖北省的省会是哪个城市?

A.长沙

B.石家庄

C.武汉

D.合肥

        每份试卷题目数量一样,但是排序随机,一共要生成几十份(根据学生数量来)。

        每道题的四个选项中的正确答案的分布也是随机的。

        因为自己手动出题的话太无聊,所以想让Python帮忙实现自动化。

本例子中只生成10份试卷及相应的答案,一共六道题(后期是可以对题目进行扩充的)

解决思路

Python自动化之试卷生成器

一、数据结构

先将问题和答案保存在一个字典中,如下:

questions={

    ‘江苏’:’南京’,

    ‘安徽’:’合肥’,

    ‘湖南’:’长沙’,

    ‘湖北’:’武汉’,

    ‘河南’:’郑州’,

    ‘河北’:’石家庄’

}

因为要使用随机,所以导入随机模块

import random

二、生成试卷

1.因为我们要生成10份试卷及答案所以需要一个循环和一个生成试卷函数(先不管试卷生成函数如何实现),先搭框架。

for paperNum in range(10):#生成10份试卷及答案

creatPaper(paperNum)#生成试卷及答案函数

2.开始写生成试卷函数:creatPaper(paperNum)

  因为该函数需要生成试卷和答案文本,所示需要用到Python的open函数来分别生成试卷文件和答案文件。

    questionPaper=open(‘地理测试试卷%s.txt’%(paperNum+1),’w’)

    answerPaper=open(‘测试试卷%s的答案.txt’%(paperNum+1),’w’)

        接下来就是要向这两个文件中写入问题和答案了,写问题的时候需要用到字典的键,比如:江苏省的省会是哪个城市?所以我们用一个列表来保存这些

provinces=list(questions.keys())

random.shuffle(provinces)

        Random.shuffle()函数是为了打乱列表中的元素,如果不打乱的话那么每张试卷的题目顺序就都一样了。

    接下来需要为每张试卷开头写上班级、姓名等信息,我们将其放在一个函数creatTitle(questionPaper,paperNum)中,先不管其如何实现。

        接下来就是要写问题了,因为一共生成六个问题所以需要一个循环:

for questionNum in range(len(questions)):#生成指定数量的题目和答案

        creatQuestion_Answer(questionPaper,answerPaper,questionNum,provinces)

        先不管creatQuestion_Answer()这个函数如何实现,其要实现的功能是向试卷中写入测试题,同时向答案文件中写入正确的答案。

最后便是关闭试题文件和答案文件:

questionPaper.close()

answerPaper.close()

完整的的代码如下:

def creatPaper(paperNum):

    questionPaper=open(‘地理测试试卷%s.txt’%(paperNum+1),’w’)

    answerPaper=open(‘测试试卷%s的答案.txt’%(paperNum+1),’w’)

    provinces=list(questions.keys())

    random.shuffle(provinces)

    creatTitle(questionPaper,paperNum)

    for questionNum in range(len(questions)):#生成指定数量的题目和答案

        creatQuestion_Answer(questionPaper,answerPaper,questionNum,provinces)

    questionPaper.close()

    answerPaper.close()

3.在生成试卷的函数中我们有两个函数还没有写

        下面先写creatTitle函数:因为这个函数需要向试卷文件中写入数据,所以需要一个试卷文件作为参数questionPaper。

        还有需要向试卷文件中写入这是第几份测试卷,所以需要一个试卷编号参数paperNum

代码如下:

def creatTitle(qPaper,paperNum):

    qPaper.write(‘-’*20+’地理测验题:%s’%(paperNum+1))

    qPaper.write(‘nn’)

    qPaper.write(‘ ‘*20)

    qPaper.write(‘姓名:n’)

    qPaper.write(‘ ‘*20)

qPaper.write(‘班级:nnn’)

        接下来就是具体写问题与答案的函数了,因为需要向试卷文件和答案文件中都写入数据,所以需要试卷文件和答案文件这两个参数,同时还需要使用questionNum和provinces来确定正确的答案。

代码如下:

def creatQuestion_Answer(qPaper,aPaper,questionNum,provinces):

    correctAnswer=questions[provinces[questionNum]]

    wrongAnswers=list(questions.values())

    del wrongAnswers[wrongAnswers.index(correctAnswer)]

    wrongAnswers=random.sample(wrongAnswers,3)

    questionAnswers=wrongAnswers+[correctAnswer]

    random.shuffle(questionAnswers)

    qPaper.write(‘%s.%s省的省会是哪个城市?n’%((questionNum+1),provinces[questionNum]))

    #下面写ABCD选项

    for i in range(4):

         qPaper.write(‘%s.%sn’%(‘ABCD’[i],questionAnswers[i]));

    #下面向答案文件中写入数据

    aPaper.write(‘%s.%sn’%((questionNum+1),’ABCD’[questionAnswers.index(correctAnswer)]))

完整的代码如下图:

Python自动化之试卷生成器

文本如下:

import random

questions={

    ‘江苏’:’南京’,

    ‘安徽’:’合肥’,

    ‘湖南’:’长沙’,

    ‘湖北’:’武汉’,

    ‘河南’:’郑州’,

    ‘河北’:’石家庄’

    }

def creatTitle(qPaper,paperNum):

    qPaper.write(‘-’*20+’地理测验题:%s’%(paperNum+1))

    qPaper.write(‘nn’)

    qPaper.write(‘ ‘*20)

    qPaper.write(‘姓名:n’)

    qPaper.write(‘ ‘*20)

    qPaper.write(‘班级:nnn’)

def creatQuestion_Answer(qPaper,aPaper,questionNum,provinces):

    correctAnswer=questions[provinces[questionNum]]

    wrongAnswers=list(questions.values())

    del wrongAnswers[wrongAnswers.index(correctAnswer)]

    wrongAnswers=random.sample(wrongAnswers,3)

    questionAnswers=wrongAnswers+[correctAnswer]

    random.shuffle(questionAnswers)

qPaper.write(‘%s.%s省的省会是哪个城市?n’%((questionNum+1),provinces[questionNum]))

    #下面写ABCD选项

    for i in range(4):

        qPaper.write(‘%s.%sn’%(‘ABCD’[i],questionAnswers[i]));

    #下面向答案文件中写入数据

    aPaper.write(‘%s.%sn’%((questionNum+1),’ABCD’[questionAnswers.index(correctAnswer)]))

def creatPaper(paperNum):

    questionPaper=open(‘地理测试试卷%s.txt’%(paperNum+1),’w’)

    answerPaper=open(‘测试试卷%s的答案.txt’%(paperNum+1),’w’)

    provinces=list(questions.keys())

    random.shuffle(provinces)

    creatTitle(questionPaper,paperNum)

    for questionNum in range(len(questions)):#生成指定数量的题目和答案

        creatQuestion_Answer(questionPaper,answerPaper,questionNum,provinces)

    questionPaper.close()

    answerPaper.close()

for paperNum in range(10):#生成10份试卷及答案

    creatPaper(paperNum)#生成试卷及答案函数

生成的文件如下图:

Python自动化之试卷生成器

Python自动化之试卷生成器

 

 

您必须 登录 才能发表评论!