用python制作多份试卷防止作弊(随机排列题目顺序和答案顺序,提供参考答案)...

#! /usr/bin/python
# randomQuizeGenerator.py   -   Creates quizzes with questions and answers in
# random order, along with the answer key.

import random

#The quize data. Keys are states and values are their capitals.
capitals = {'Alabama':'Montgomery', 'Alaska':'Juneau','Arizona':'Phoenix','Arkansas':'Little Rock', 'California':'Sacramento'}

#Generate 3 quiz files.
for quizNum in range(3):
    #Create the quiz and answer key files.
    quizFile = open('capitalsquiz%s.txt'%(quizNum+1),'w')
    answerKeyFile = open('capitalsquiz_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 5 states, making a question for each.
    for questionNum in range(5):
        #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 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'%(option[i])%(answerOptions[i]))
            quizFile.write('%s.'%('ABCD'[i]))
            quizFile.write('%s  \n'%(answerOptions[i]))
        quizFile.write('\n')
        #TODO: Write the answer key to a file
        answerKeyFile.write('%s.%s\n'%((questionNum+1),'ABCD'[answerOptions.index(correctAnswer)]))
    quizFile.close()
    answerKeyFile.close()

转载于:https://www.cnblogs.com/guolongnv/p/8136464.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值