python 随机生成一个Jeopardy!®风格的测验

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 随机生成一个Jeopardy!®风格的测验相关的知识,希望对你有一定的参考价值。

#! python2
# -*- coding: cp1252 -*-
# randomQuizGenerator-Jeopardy.py - Creates quizzes with questions and answer in
# random order, along with the answer key

import random

#The number of quizes to generate
quizzQty = 1

#The quiz data; keys are questions and values are answers
quizQuestions = {"The work's reflection of your attitude to your subject will come out as mood or this other 4-letter word": "tone",
                 "Wish I had a nickel for every time I've told authors to avoid this type of expression; it's from the French for \"stereotype\"": "cliché",
                 "From the Greek for \"first competitor\", it's your leading character": "protagonist",
                 "You can't have much character development without it; \"of interest\" is another type": "conflict",
                 "Try to inspire this release of emotions like fear or pity, a term used by Aristotle": "catharsis",
                 "It can mean \"full of life\", or describe characters like Rocky & Bullwinkle": "animated",
                 "Above 66 degrees 33 minutes north latitude": "Arctic",
                 "Done without thinking or shifting": "automatic",
                 "Smelly, but in a good way": "aromatic",
                 "Los Angeles is classified as \"semi-\" this condition of little rain": "arid",
                 "In 1604 King James I called this plant \"dangerous to the lungs\"": "tobacco",
                 "There are 4 varieties of this deadly African venomous snake, 3 green & 1 black": "a mamba",
                 "This swimming superstar made many a movie right here at Sony studios, including 1953's \"Dangerous When Wet\"": "Esther Williams",
                 "Madame de Merteuil must flee after Parisians learn of her machinations in this 1782 novel": "Les Liaisons dangereuses\n    (or Dangerous Liaisons)",
                 "This stormy point at the bottom of South America has a monument dedicated in 1992 to sailors lost trying to round it": "Cape Horn",
                 "\"Insanely great\" was how Steve described this breakthrough computer when it was introduced in 1984": "Macintosh",
                 "Steve Jobs started his oft-quoted 2005 commencement at this Cal. univ. by telling why he dropped out of Reed College": "Stanford",
                 "Steve Jobs joined this other Steve in 1976 to design the logic board that would become known as Apple I": "Wozniak",
                 "After rejoining Apple in 1997, Jobs engineered this 2-word ad campaign that opposed a lockstep mentality": "\"Think Different\"",
                 "In the 1980s Jobs began NeXT company with help from this Texas entrepreneur & future presidential candidate": "Ross Perot",
                 "It's defined as the period between sunset & sunrise": "night",
                 "This sweet word can precede suckle, dew & moon": "honey",
                 "To kick back, or the title of a 1985 hit by Frankie goes to Hollywood": "relax",
                 "Musical instrument whose name is Hindi for \"3-stringed\", tho it actually has more": "sitar",
                 "Salix babylonica is commonly called this due to its drooping leaves & branches": "a weeping willow"
                 }

#The number of questions to generate per quiz
questionCount = len(quizQuestions)/2

#Generate a quiz question for each question.
for quizNum in range(quizzQty):
    #Create the quiz and answer key files.
    quizFile = open('quiz%s.txt' % (quizNum + 1), 'w')
    answerKeyFile = open('quiz_answers%s.txt' % (quizNum + 1), 'w')

    #Write out a header for the quiz
    quizFile.write('Name:\n\n')
    quizFile.write((' '*20) + 'Jeopardy Quiz (Form %s)' % (quizNum + 1))
    quizFile.write('\n\n')

    #Shuffle the order of the Questions
    questions = list(quizQuestions.keys())
    random.shuffle(questions)

    #Loop through all states, making a question for each
    for questionNum in range(questionCount):
        #Get right and wrong answers
        correctAnswer = quizQuestions[questions[questionNum]]
        wrongAnswers = list(quizQuestions.values())
        del wrongAnswers[wrongAnswers.index(correctAnswer)]
        wrongAnswers = random.sample(wrongAnswers, 4)
        answerOptions = wrongAnswers + [correctAnswer]
        random.shuffle(answerOptions)

        #Write the question and the answer options to the quiz file.
        quizFile.write('%s. %s?\n' % (questionNum + 1, questions[questionNum]))
        for i in range(5):
            quizFile.write('\t%s. %s\n' % ('ABCDE'[i], answerOptions[i]))

        quizFile.write('\n')

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

    #Close out the files
    quizFile.close()
    answerKeyFile.close()

以上是关于python 随机生成一个Jeopardy!®风格的测验的主要内容,如果未能解决你的问题,请参考以下文章

python随机生成两个数字

python用生成随机数的方法

怎么用python生成随机数?

怎么用python生成随机的且不重复的整数?

python随机生成一个4位整数,求各个数位上的数字之和

python 生成随机字符串