假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验。不妙的是,班里有几个坏蛋,你无法确信学生不会作弊。你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人都不能从其他人那里抄袭答案。当然,手工完成这件事又费时又无聊。好在,你懂一些 Python。

下面是程序所做的事:

· 创建 35 份不同的测验试卷。

· 为每份试卷创建 50 个多重选择题,次序随机。

· 为每个问题提供一个正确答案和 3 个随机的错误答案,次序随机。

· 将测验试卷写到 35 个文本文件中。

· 将答案写到 35 个文本文件中。

这意味着代码需要做下面的事:

· 将州和它们的首府保存在一个字典中。

· 针对测验文本文件和答案文本文件,调用 open()、write()和 close()。

· 利用 random.shuffle()随机调整问题和多重选项的次序。

第 1 步:将测验数据保存在一个字典中

第一步是创建一个脚本框架,并填入测验数据。创建一个名为 randomQuizGenerator.py 的文件,让它看起来像这样:

#! python3

# randomQuizGenerator.py - Creates quizzes with questions and answers in

# random order, along with the answer key.

import random

# The quiz data. Keys are states and values are their capitals.

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'}

# Generate 35 quiz files.

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 50 states, making a question for each.

因为这个程序将随机安排问题和答案的次序,所以需要导入 random 模块,以便利用其中的函数。capitals 变量含一个字典,以美国州名作为键,以州首府作为值。因为你希望创建 35 份测验试卷,所以实际生成测验试卷和答案文件的代码(暂时用 TODO 注释标注)会放在一个 for 循环中,循环 35 次(这个数字可以改变,生成任何数目的测验试卷文件)。

第 2 步:创建测验文件,并打乱问题的次序

现在是时候填入那些 TODO 了。

循环中的代码将重复执行 35 次(每次生成一份测验试卷),所以在循环中,你只需要考虑一份测验试卷。首先你要创建一个实际的测验试卷文件,它需要有唯一的文件名,并且有某种标准的标题部分,留出位置,让学生填写姓名、日期和班级。然后需要得到随机排列的州的列表,稍后将用它来创建测验试卷的问题和答案。在 randomQuizGenerator.py 中添加以下代码行:

#! python3

# randomQuizGenerator.py - Creates quizzes with questions and answers in

# random order, along with the answer key.

--snip--

# Generate 35 quiz files.

for quizNum in range(35):

# Create the quiz and answer key files.

quizFile = open('captalsquiz%s.txt' % (quizNum + 1), 'w')

answerKeyFile = open('captalsquiz_answers%s.txt' % (quizNum + 1), 'w')

# Write out the header for the quiz.

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 the states.

states = list(capitals.keys())

random.shuffle(states)

# TODO: Loop through all 50 states, making a question for each.

测验试卷的文件名将是capitalsquiz.txt,其中是该测验试卷的唯一编号,来自于 quizNum,即 for 循环的计数器。针对 capitalsquiz.txt 的答案将保存在一个文本文件中,名为 capitalsquiz_answers.txt。每次执行循环,'capitalsquiz%s.txt'和'capitalsquiz_answers%s.txt'中的占位符%s 都将被(quizNum + 1)取代,所以第一个测验试卷和答案将是 capitalsquiz1.txt 和 capitalsquiz_answers1.txt。在quizFile和answerKeyFile的 open()函数调用将创建这些文件,以'w'作为第二个参数,以写模式打开它们。 write()语句创建了测验标题,让学生填写。最后,利用 random.shuffle()函数,创建了美国州名的随机列表。该函数重新随机排列传递给它的列表中的值。

第 3 步:创建答案选项

现在需要为每个问题生成答案选项,这将是 A 到 D 的多重选择。你需要创建另一个 for 循环,该循环生成测验试卷的 50 个问题的内容。然后里面会嵌套第三个for 循环,为每个问题生成多重选项。让你的代码看起来像这样:

#! python3

# randomQuizGenerator.py - Creates quizzes with questions and answers in

# random order, along with the answer key

--snip--

# Loop through all 50 states, making a question for each.

for questionNum in range(50):

# Get right and wrong answers.

correctAnswer = capitals[states[questionNum]]

wrongAnswers = list(capitals.values())

del wrongAnswers[wrongAnswers.index(correctAnswer)]

wrongAnswers = random.sample(wrongAnswers, 3)

answerOptions = wrongAnswers + [correctAnswer]

random.shuffle(answerOptions)

# TODO: Write the question and the answer options to the quiz file.

# TODO: Write the answer key to a file.

正确的答案很容易得到,它作为一个值保存在 capitals 字典中。这个循环将遍历打乱过的 states 列表中的州,从 states[0]到 states[49],在 capitals 中找到每个州,将该州对应的首府保存在 correctAnswer 中。可能的错误答案列表需要一点技巧。你可以从capitals字典中复制所有的值,删除正确的答案,然后从该列表中选择 3 个随机的值。random.sample()函数使得这种选择很容易,它的第一个参数是你希望选择的列表,第二个参数是你希望选择的值的个数。完整的答案选项列表是这 3 个错误答案与正确答案的组合。最后 ,答案需要随机排列,这样正确的答案就不会总是选项 D。

第 4 步:将内容写入测验试卷和答案文件

剩下来就是将问题写入测验试卷文件,将答案写入答案文件。让你的代码看起来像这样:

#! python3

# randomQuizGenerator.py - Creates quizzes with questions and answers in

# random order, along with the answer key.

--snip--

# Loop through all 50 states, making a question for each.

for questionNum in range(50):

--snip--

# Write the question and the answer options to the quiz file.

quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))

for i in range(4):

quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))

quizFile.write('\n')

# Write the answer key to a file.

answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))

quizFile.close()

answerKeyFile.close()

一个遍历整数 0 到 3 的 for 循环,将答案选项写入 answerOptions 列表。表达式'ABCD'[i]将字符串'ABCD'看成是一个数组,它在循环的每次迭代中,将分别求值为'A'、'B'、'C'和'D'。在最后一行,表达式 answerOptions.index(correctAnswer)将在随机排序的答案选项中,找到正确答案的整数下标,并且'ABCD'[answerOptions.index(correctAnswer)]将求值为正确答案的字母,写入到答案文件中。在运行该程序后,下面就是 capitalsquiz1.txt 文件看起来的样子。但是,你的问题和答案选项当然与这里显示的可能会不同。这取决于random.shuffle()调用的结果:

对应的 capitalsquiz_answers1.txt 文本文件看起来像这样:

完整代码:

#! python3

# randomQuizGenerator.py - Creates quizzes with questions and answers in

# random order, along with the answer key.

import random

# The quiz data. Keys are states and values are their capitals.

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'}

# Generate 35 quiz files.

for quizNum in range(35):

# Create the quiz and answer key files.

quizFile = open('captalsquiz%s.txt' % (quizNum + 1), 'w')

answerKeyFile = open('captalsquiz_answers%s.txt' % (quizNum + 1), 'w')

# Write out the header for the quiz.

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 the states.

states = list(capitals.keys())

random.shuffle(states)

# Loop through all 50 states, making a question for each.

for questionNum in range(50):

# Get right and wrong answers.

correctAnswer = capitals[states[questionNum]]

wrongAnswers = list(capitals.values())

del wrongAnswers[wrongAnswers.index(correctAnswer)]

wrongAnswers = random.sample(wrongAnswers, 3)

answerOptions = wrongAnswers + [correctAnswer]

random.shuffle(answerOptions)

# Write the question and the answer options to the quiz file.

quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))

for i in range(4):

quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))

quizFile.write('\n')

# Write the answer key to a file.

answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))

quizFile.close()

answerKeyFile.close()

python生成随机的测验试卷_python生成随机的测验试卷文件相关推荐

  1. python生成10个随机数字符串_python生成随机数、随机字符串

    python生成随机数.随机字符串 import random import string # 随机整数: print random.randint(1,50) # 随机选取0到100间的偶数: pr ...

  2. 基于python的随机森林回归实现_python实现随机森林

    定义: 随机森林指的是利用多棵决策树对样本进行训练并预测的一种分类器.可回归可分类. 所以随机森林是基于多颗决策树的一种集成学习算法,常见的决策树算法主要有以下几种: 1. ID3:使用信息增益g(D ...

  3. python随机森林变量重要性_Python中随机森林的实现与解释

    使用像Scikit-Learn这样的库,现在很容易在Python中实现数百种机器学习算法.这很容易,我们通常不需要任何关于模型如何工作的潜在知识来使用它.虽然不需要了解所有细节,但了解机器学习模型是如 ...

  4. python随机生成四位验证码的代码_Python random随机生成6位验证码示例代码

    随机生成6位验证码代码 # -*- coding: utf-8 -*- import random def generate_verification_code(): ''' randomly gen ...

  5. python大乐透号码生成器_Python生成随机验证码,大乐透号码

    随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__': check_code = "" # ...

  6. python随机红包怎么发_python生成随机红包的实例写法

    假设红包金额为money,数量是num,并且红包金额money>=num*0.01 原理如下,从1~money*100的数的集合中,随机抽取num-1个数,然后对这些数进行排序,在排序后的集合前 ...

  7. python生成10000个样本数据集_python产生随机样本数据

    一.产生X样本 x_train = np.random.random((5, 3)) 随机产生一个5行3列的样本矩阵,也就是5个维度为3的训练样本. array([[ 0.56644011, 0.75 ...

  8. python语言编写一个生成九宫格图片的代码_Python 生成你的朋友圈九宫格图片

    关于微信之前写过以下文章,有兴趣可以点击查看: 你可能在朋友圈看过九宫格图片(把一张图片按照比例分成九份),就像这样的: 还有微博九宫格图 https://weibo.com/2717930601/. ...

  9. python给pdf加图片签名_Python生成个性签名图片获取GUI过程解析

    这篇文章主要介绍了Python生成个性签名图片获取GUI过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先来看看程序运行的样子: 所以,程序 ...

最新文章

  1. 0基础学python看什么书-零基础学python编程需要看什么书?
  2. python爬虫教程(一)
  3. linux 操作mysql 数据库命令_在Linux上用命令怎么连接数据库
  4. 计算机网络技术通识试题,超星计算机网络技术章节答案
  5. java泛型程序设计——泛型类型的继承原则
  6. java cassandra连接池_java操作cassandra(连接池)
  7. Java 读取Oracle数据库中的Date日期型怎么去掉秒后面的0
  8. Java前端技术汇总
  9. 下载Chrome历史版本
  10. OMRON-FINS(TCP)协议详细解析和攻击
  11. python中如何将矩阵合并并多一个维度
  12. 【OpenGL开发】关于GLEW扩展库
  13. android swap 大小,android 手机内存SWAP经验
  14. 装了svn桌面右键没有_右键菜单没有svn选项怎么办|win7 svn没有右键菜单怎么解决|svn添加到右键菜单方法...
  15. 业务后台商业组件ViewUI(iView)入门
  16. linux服务器新装hba卡,EmulexHBA卡在Linux下的安装方法
  17. 无符号数与带符号数的相加减
  18. ROS1/2 机器人编程实践汇总 kinetic/melodic/noetic foxy/galactic/humble
  19. wc 一个进程结果是2_用开放的wc创建一个Web组件
  20. Redis 安装配置开机启动整合SpringBoot以及配置文件详解

热门文章

  1. Docker网络模式解析
  2. 数据库系统概论(第四版)习题解答
  3. html5类选择器用什么表示,HTML_揭秘常用的五类CSS选择器用法,有许多新手朋友不知道在什么 - phpStudy...
  4. mysql同时更新2个表_mysql中同时update更新多个表
  5. 电赛2019年F题纸张测量FDC2214的初始化代码(含STM32f103zet6和f103c8t6)胎教式
  6. IPSec 基础介绍
  7. 平面设计的表现手法有哪些比较常用
  8. 中软国际首届嘉年华晚会召开 “解放号”勿忘初心再起航
  9. 计算机专业英语教学重难点,浅析计算机专业英语的教学现状及对策
  10. 洛谷P1562 还是N皇后(DFS+状态压缩+位运算)