生成随机的测验试卷文件

##########################################################
# 生成随机的测验试卷文件
#! python3
# randomQuizGenerator.py - Create 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 capital.
capitals = {
    'HeBei':'ShiJiaZhuang',
    'ShanXi':'TaiYuan',
    'LiaoNing':'ShenYang',
    'JiLin':'ChangChun',
    'HeiLongJiang':'HaErBin',
    'JiangSu':'NanJing',
    'ZheJiang':'HangZhou',
    'AnHui':'HeFei',
    'GuangDong':'ShenZhen',
}
#print(capitals)

# Generate 35 quiz files.
for quizNum in range(35):
    # TODO: Create the quiz and answer key files
    quizFile = open('E:\\Pycharm\\Python编程快速上手---让繁琐工作自动化\\capitalquiz%s.txt' % (quizNum + 1),'w')
    answerKeyFile = open('E:\\Pycharm\\Python编程快速上手---让繁琐工作自动化\\capitalquiz_answers%s.txt' % (quizNum + 1),'w')

    # TODO: 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')

    # TODO: Shuffle the order of the states.
    states = list(capitals.keys())
    random.shuffle(states)

    # TODO: Loop through all 9 states, making a question for each.
    for questionNum in range(9):
        # Get right and wrong answers.
        correntAnswer = capitals[states[questionNum]]
        wrongAnswer = list(capitals.values())
        del wrongAnswer[wrongAnswer.index(correntAnswer)]
        wrongAnswer = random.sample(wrongAnswer,3)
        answerOptions = wrongAnswer + [correntAnswer]
        random.shuffle(answerOptions)

        # TODO: Write the question and answer options to the quiz file
        quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1,states[questionNum]))
        # ABCD
        for i in range(4):
            quizFile.write(' %s. %s\n' % ('ABCD'[i],answerOptions[i]))
        quizFile.write('\n')

        # TODO: Write the answer key to a file.
        answerKeyFile.write('%s. %s\n' % (questionNum + 1,'ABCD'[answerOptions.index(correntAnswer)]))

quizFile.close()
answerKeyFile.close()

##########################################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值