#! python3 #在指定的路径中生成指定数目的测验试卷和参考答案文本,试卷内容是测验对我国28个省级行政区的省会城市的选择, #要求每份试题的试题顺序不同,不同试卷中相同试题的选项随机抽取。 import random import os # 各省及其省会的字典数据 capitals = {‘新疆‘: ‘乌鲁木齐‘, ‘甘肃‘: ‘兰州‘, ‘宁夏‘: ‘银川‘, ‘陕西‘: ‘西安‘, ‘青海‘: ‘西宁‘, ‘西藏‘: ‘拉萨‘, ‘贵州‘: ‘贵阳‘, ‘内蒙古‘: ‘呼和浩特‘, ‘山西‘: ‘大同‘, ‘山东‘: ‘济南‘, ‘河南‘: ‘郑州‘, ‘河北‘: ‘石家庄‘, ‘黑龙江‘: ‘哈尔滨‘, ‘吉林‘: ‘长春‘, ‘辽宁‘: ‘沈阳‘, ‘江苏‘: ‘南京‘, ‘安徽‘: ‘合肥‘, ‘四川‘: ‘成都‘, ‘福建‘: ‘福州‘, ‘湖南‘: ‘长沙‘, ‘湖北‘: ‘武汉‘, ‘广东‘: ‘广州‘, ‘广西‘: ‘南宁‘, ‘云南‘: ‘昆明‘, ‘海南‘: ‘海口‘, ‘台湾‘: ‘台北‘, ‘浙江‘: ‘杭州‘, ‘江西‘: ‘南昌‘} #在 D 盘创建路径以存储测试题 quizePath = ‘D:\\测试题库\\地理库\\省会城市测试库\\‘ if not os.path.exists(quizePath): os.makedirs(‘D:\\测试题库\\地理库\\省会城市测试库\\‘) #在指定文件夹下生成49份测试题和相应的参考答案 for quizeNum in range(49): #生成试题和答案文件 quizeFile = open(quizePath + ‘省会测试{}.txt‘.format(quizeNum + 1), ‘w‘) answerKeyFile = open(quizePath + ‘省会测试答案{}.txt‘.format(quizeNum + 1), ‘w‘) #生成试题卷头 quizeFile.write(‘姓名:\n\n日期:\n\n周期:\n\n‘) quizeFile.write(‘ ‘*23 + ‘我国省会测试题(第{}套)‘.format(quizeNum + 1)) quizeFile.write(‘\n\n‘) #随机排列各省顺序 province = list(capitals.keys()) random.shuffle(province) for questionNum in range(len(capitals)): #创建答案和选项 correctAnswer = capitals[province[questionNum]] wrongAnswers = list(capitals.values()) del wrongAnswers[wrongAnswers.index(correctAnswer)] wrongAnswers = random.sample(wrongAnswers, 3) answerOptions = wrongAnswers + [correctAnswer] random.shuffle(answerOptions) #在试卷中写入问题和答案选项 quizeFile.write(‘{0}、{1}的省会城市是?\n‘.format(questionNum + 1, province[questionNum])) for i in range(4): quizeFile.write(‘{0}、{1}\n‘.format(‘ABCD‘[i], answerOptions[i])) quizeFile.write(‘\n‘) #在参考答案中写入正确答案 answerKeyFile.write(‘%s、%s\n‘ % (questionNum + 1, ‘ABCD‘[answerOptions.index(correctAnswer)])) #当每一份试卷和参考答案创建完毕后关闭相应的文件 quizeFile.close() answerKeyFile.close()
原文地址:https://www.cnblogs.com/shanger/p/12178055.html
时间: 2024-10-28 20:32:05