生成了5份试卷和5份答案,效果如图
每份试卷有名字和日期,以及50道相似的选择题,就是选择美国一个洲的首府是哪一个
比如第一题Wisconsin的首府就是A.Midison
我之前曾经用java做过类似的试卷生成系统,代码量是python的3倍左右!
python实现需要的知识主要是文件读写以及random库的基本使用
思路
外面套一个for循环,次数就是生成的试卷数
然后在里面创建两个文件,试卷文件和答案文件
试卷文件
写入姓名,日期,试卷编号
套一个for循环,次数是题目个数
写入问题,问题中的“洲”那个单词(就是最后一个单词:如 1 what is the capital of
Wisconsin)用一个列表来储存,然后随机放置
for循环,次数为4,表示4个选项
写入答案,答案也是一个列表,用正确答案和错误答案啊混合而成,再随机一遍
答案文件
将正确答案对应的选项填入即可
代码分析
首先是资源字典,我们所有的题目素材都在里面,key是洲,value是对应的首府
import random
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix',
'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver',
'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee',
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois':
'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':
'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine':
'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan':
'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri':
'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':
'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New'
'Mexico': 'Santa Fe','New York': 'Albany', 'North Carolina': 'Raleigh',
'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',
'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence',
'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee':
'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont':
'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West'
'Virginia': 'Charleston','Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'
}
这是创建两种文件
for i in range(5):
Question = open('paper%s.txt' % (i+1), 'w')
Answer = open('answer%s.txt' % (i+1), 'w')
#将洲放在states中,以便生成问题中的关键词
states = list(capitals.keys())
#打乱states列表的顺序
random.shuffle(states)
#写下试卷头部部分
Question.write('Name :\ndate : \n')
Question.write((' ' * 20) + 'paper%s\n' % (i+1))
接下来是写题目,也是本次代码的核心部分
# 打印问题
for question in range(50):
Question.write('%s what is the capital of %s' % ((question + 1), states[question]))
Question.write('\n')
# 选项
correctAnswer = capitals[states[question]]
#错误答案是有全体答案减去正确答案生成,并且只随机取其中3个
wrongAnswer = list(capitals.values())
del wrongAnswer[wrongAnswer.index(correctAnswer)]
wrongAnswer = random.sample(wrongAnswer, 3)
#然后混合两种答案,但是我们依然可以通过answerOptions的索引重新找到正确答案,生成答案文件
answerOptions = wrongAnswer + [correctAnswer]
#打乱答案顺序,避免正确答案位置固定
random.shuffle(answerOptions)
#根据选项总数量写下选项
for answer in range(len(answerOptions)):
Question.write('%s.%s\n' % ('ABCD'[answer], answerOptions[answer]))
Question.write('\n')
源代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/2/17 17:39
# @Author : cs
# @decription 随机生成试卷
import random
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix',
'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver',
'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee',
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois':
'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':
'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine':
'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan':
'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri':
'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':
'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New'
'Mexico': 'Santa Fe','New York': 'Albany', 'North Carolina': 'Raleigh',
'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',
'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence',
'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee':
'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont':
'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West'
'Virginia': 'Charleston','Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'
}
# 创建文件
for i in range(5):
Question = open('paper%s.txt' % (i+1), 'w')
Answer = open('answer%s.txt' % (i+1), 'w')
states = list(capitals.keys())
random.shuffle(states)
Question.write('Name :\ndate : \n')
Question.write((' ' * 20) + 'paper%s\n' % (i+1))
# 打印问题
for question in range(50):
Question.write('%s what is the capital of %s' % ((question + 1), states[question]))
Question.write('\n')
correctAnswer = capitals[states[question]]
wrongAnswer = list(capitals.values())
del wrongAnswer[wrongAnswer.index(correctAnswer)]
wrongAnswer = random.sample(wrongAnswer, 3)
answerOptions = wrongAnswer + [correctAnswer]
random.shuffle(answerOptions)
for answer in range(len(answerOptions)):
Question.write('%s.%s\n' % ('ABCD'[answer], answerOptions[answer]))
Question.write('\n')
Answer.write('%s.%s\n' % (question + 1, 'ABCD'[answerOptions.index(correctAnswer)]))