zoukankan      html  css  js  c++  java
  • 用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: Date: Period: ')
        quizFile.write((' '*20)+'State Capitals Quiz (Form %s)'%(quizNum + 1))
        quizFile.write(' ')

        #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 ? '%((questionNum+1),states[questionNum]))
            for i in range(4):
    #            quizFile.write('%s %s '%(option[i])%(answerOptions[i]))
                quizFile.write('%s.'%('ABCD'[i]))
                quizFile.write('%s  '%(answerOptions[i]))
            quizFile.write(' ')
            #TODO: Write the answer key to a file
            answerKeyFile.write('%s.%s '%((questionNum+1),'ABCD'[answerOptions.index(correctAnswer)]))
        quizFile.close()
        answerKeyFile.close()

  • 相关阅读:
    【原创】解决向工程中添加Megacore 文件在文件列表中没有出现目标文件的问题
    (笔记)找工作,该怎么进补
    (原创)结构体位域操作
    (原创)TCP/IP学习笔记之IP(网际协议)
    (原创)确认大端模式或小端模式(最直接有效的方法)
    (原创)HDL中的unsigned与signed
    (原创)TCP/IP学习笔记之概述
    (笔记)往一个指定的地址读写一个值
    (笔记)我的EDN博客被评为专家博客啦
    (原创)同步复位与异步复位
  • 原文地址:https://www.cnblogs.com/guolongnv/p/8136464.html
Copyright © 2011-2022 走看看