如何用python自动改试卷_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)#生成试卷及答案函数

|

生成的文件如下图:

8-1540878070.jpg

9-1540878071.jpg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值