我帮王老师解决了随机产生试卷的烦恼。

王老师是一个老师,也是我的老师。现在他想考考他的学生对于历史常识的问题。但是他又想避免学生们相互抄袭。但是他们班有35个学生。如果手动去分配试卷将会是既枯燥,又有巨大工作量的事情。于是王老师找到我了:

提出需求:

1.创建35份不同的试卷。

2.为每份试卷创建30道多重选择题,次序随机

3.为每个选择题创建一个正确的选择题和三个错误的选择题。

4.将测试试卷写到35个文件中。

5.将测试答案也写到35个试卷里面。

从以上需求,我们先罗列一下知识点,文件的系统open() ,write(),random模块中的.shuffle()和sample()函数。shuffle()将用来打乱题目的顺序。sample将用来随机的获取三个错误的答案。

第一步:当然是定好程序设计的框架基调了。

import random 

import os

capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phenix',

            'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denvir',
'Connecticut': 'Hartford', '唐朝': '长安', 'Delaware': 'Dover',
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise',
'宋朝': '东京开封', 'Kengtucky': 'Frankfort', 'Louisiana': 'Baton Rouge',
'明朝': '北京', '元朝': '大都', 'Iowa': 'Des Moines', 'Massachusetts': 'Boston',
'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson',
'Missiouri': 'Jefferson City', 'Montana': 'Helea', 'Nebraska': 'Lincoln',
'Nevada': 'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton',
'Mexico': 'Santa Fe', 'New York': 'Albany'}#例子我随便举的 你们随意

def domain():

  #首先我们产生的试卷文件和答案不能随机摆放吧 显得太乱 那是女生寝室才该有的样子

  path='save_folderName'

  if not os.paht.exists(path):#首先判断路径是否存在不存在那么重新建立一个  然后在把工作路径放到这个新建的路径里面

    os.mkdir(path)

  os.chdir(path)

  #generate 35 quiz files for everyone

  for quiznum in range(35):
    #TODO:Create the quiz and answer key files 首先创建保存文件名啊

    #TODO:Write out the header for the quiz 把题目构建好

    #TODO:Shuffle the order of the states 打乱题目的顺序

    #TODO:Loop through all 30 states,making a question for each 按照循环顺序一份试卷出炉。

然后就到了我贴代码的时候了 :

#!python3
#randomQuizGenerators.py --Create quizzs with questions and answers
#in random order,along with the answer key
#31-12-2018 by kingtao
import random
import os

capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phenix',
'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denvir',
'Connecticut': 'Hartford', '唐朝': '长安', 'Delaware': 'Dover',
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise',
'宋朝': '东京开封', 'Kengtucky': 'Frankfort', 'Louisiana': 'Baton Rouge',
'明朝': '北京', '元朝': '大都', 'Iowa': 'Des Moines', 'Massachusetts': 'Boston',
'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson',
'Missiouri': 'Jefferson City', 'Montana': 'Helea', 'Nebraska': 'Lincoln',
'Nevada': 'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton',
'Mexico': 'Santa Fe', 'New York': 'Albany'}


def domain():
#work path
path='exams'
if not os.path.exists(path):
os.mkdir(path)
os.chdir(path)
#quiz data .Keys are states and values are their capitals
for quiznum in range(35):
#create quizzs and answers
quizfile=open('quizfile_%s.txt'%(quiznum+1),'w',encoding='utf-8')
answerfile=open('answerfile_%s.txt'%(quiznum+1),'w',encoding='utf-8')

#name date period
quizfile.write('Name:\n\nDate:\n\nPeriod\n\n')
quizfile.write((' '*20)+'State Capitals Quiz(Form %s)'%(quiznum+1))
quizfile.write('\n\n')

#shuffle the order of states
states=list(capitals.keys())
print(type(states))
random.shuffle(states)
#make exercises
for num in range(30):
#create a correct answer and 3 wrong answer
correctAnswer=capitals[states[num]]
wrongAnswer=list(capitals.values())
del wrongAnswer[wrongAnswer.index(correctAnswer)]
wrongAnswer=random.sample(wrongAnswer,3)
answeroptions=wrongAnswer+[correctAnswer]#wrongAnswer.append(correctAnswer)
random.shuffle(answeroptions)

#write the questions and answers
quizfile.write('%s.What is the capital of %s?\n'%(num+1,states[num]))
for i in range(0,3,2):
for j in range(i,i+2):
quizfile.write('%4s.%s'%('ABCD'[j],answeroptions[j]))
quizfile.write('\n')
#correct answers
answerfile.write('%s.'%(num+1)+'ABCD'[answeroptions.index(correctAnswer)]+'\n')
quizfile.close()
answerfile.close()
# break






if __name__=='__main__':
domain()
这个试卷只有选择题 如果你需要判断题 问答题 请参考选择题部分自己分类啊 不难吧 so easy!

猜你喜欢

转载自www.cnblogs.com/Aljt/p/10204708.html