首页 > 解决方案 > 自动化无聊的东西:随机测验生成器

问题描述

我一直在关注一本教程书中的示例程序,该程序是获取一本包含所有 50 个美国州及其首府的字典,然后创建一组随机的多项选择 AD 问题,然后将这些问题随机化,然后 3不同的测验打印成 3 个不同的文件。然后将每个测验的所有问题的答案打印到答案文件中,以与每个问题文件一起使用。

作为一项测试,我现在只使用 5 的范围进行测试。当我运行程序时,程序按预期工作,除了每个测试只创建 25 个问题/答案组合,而不是 50 个。

我已经检查了几次,无法弄清楚这是为什么。任何输入将不胜感激,谢谢。

# randomQuizGenerator.py - Creates quizzes with questions and answers in
# random order, along with the answer key.

import random

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 5 quiz files.
for quizNum in range(5):
    # Create the quiz and answer key files.
    quizFile = open('capitalsquiz%s.txt' % (quizNum+1), 'w')
    answerFile = open('capitalsquiz_answers%s.txt' % (quizNum+1), 'w')

    # Write out the header for the quiz.
    quizFile.write('Capitals Quiz #%s' % (quizNum+1) + '\nName:\nDate:\n\n')
    quizFile.write('What is the capital of:\n')
    answerFile.write('Capitals Quiz %s' % (quizNum+1) + '\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.
    # set question number = 0
    q_num = 0
    for st in states:
        # question number increase
        q_num += 1
        random.shuffle(states)

        # unused needed for choosing 3 incorrect options
        unusedStates = states

        # write question number and state name (QUESTION)
        quizFile.write('Q%s: ' % q_num + st + '?\n')

        # create answer options list and fill with 1 correct answer + 3 incorrect ones
        answerOptions = [None] * 3
        answerOptions.append(capitals[st])
        # remove correct answer to avoid duplication
        unusedStates.remove(st)
        for Opt in range(0, 3):
            curr_ans = unusedStates[Opt]
            answerOptions[Opt] = capitals[curr_ans]
        # randomise answer list
        random.shuffle(answerOptions)
        # write answers
        for i in range(0, 4):
            quizFile.write(answerOptions[i]+'   ')
        quizFile.write('\n')

        # write correct answer in answer file
        answerFile.write(capitals[st]+'\n')

    quizFile.close()
    answerFile.close()

标签: pythonpython-3.x

解决方案


@hlfrmn 找到了罪魁祸首——我想指出另一件事——使用

with open("filename.txt","w") as f:
    f.write("something")

即使遇到异常也会自动关闭文件的方法,并使用执行某些任务的函数对其进行结构化。

数据定义:

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

和代码:

import random

def generateAllQuestions(caps):
    """Generates all questions including 3 wrong answers. Returns a list of
     [tuple(state,correct) and list of 3 wrong answers]."""
    q = []
    for state in capitals:
        # get 4 other answers 
        # remove the correct one if it is inside the random sample
        # use only 3 of them
        others = [ val for key,val in random.sample(capitals.items(),k=4) if key != state][0:3]

        # compile [tuple: (item,correct_answer),[other_answers]]
        q.append([(state,capitals[state])] + [others])

    return q

def partitionIntoNParts(n,data):
    """Takes the data and partiniones it into n random equally long (if possible)
    sublists"""
    ld = len(data)
    size_part = ld // n
    idx = 0
    random.shuffle(data)
    while idx < ld:
        yield data[idx:idx + size_part]
        idx += size_part

def writeHeader(f,a,n):
    """Write the header for Q and A file"""
    a.write(f"Capitals Quiz #{n+1}\n\n")
    f.write(f"Capitals Quiz #{n+1}\nName:\nDate:\n\nWhat is the capital of:\n")

def writeQandA(f,a,q_num,q):
    """Write a single questions into Q-file and a single answer into A-file"""
    state,correct = q[0] # the tuple
    others = q[1]        # the others

    a.write(f"{q_num+1:>3}.) {state:<14} : {correct}\n")
    f.write(f"{q_num+1:>3}.) {state:<14} : ")
    solutions = others + [correct]
    random.shuffle(solutions) # use sort() to always get alphabetical order
    for town in solutions:
        f.write(f"[ ] {town:<14}    ")
    f.write("\n\n")


# how many files to use?
filecount = 5

qs = generateAllQuestions(capitals)

parts = partitionIntoNParts(filecount,qs)

# write files based on partioning 
for idx,content in enumerate(parts):
    with open(f"capitalsquiz{idx+1}.txt","w") as quiz_file,\
        open(f"capitalsquiz{idx+1}_answers.txt","w") as answ_file:

        writeHeader(quiz_file,answ_file,idx)
        # write Q and A into file
        for q_num,q in enumerate(content):
            writeQandA(quiz_file,answ_file,q_num,q)

# check one files content:
print(open("capitalsquiz2.txt").read())
print(open("capitalsquiz2_answers.txt").read())

内容capitalsquiz2.txt

Capitals Quiz #2
Name:
Date:

What is the capital of:
  1.) Oklahoma       : [ ] Oklahoma City     [ ] Phoenix           [ ] Juneau            [ ] Olympia

  2.) Virginia       : [ ] Austin            [ ] Pierre            [ ] Saint Paul        [ ] Richmond

  3.) North Carolina : [ ] Raleigh           [ ] Tallahassee       [ ] Dover             [ ] Harrisburg

  4.) Montana        : [ ] Helena            [ ] Raleigh           [ ] Hartford          [ ] Madison

  5.) Alaska         : [ ] Nashville         [ ] Albany            [ ] Juneau            [ ] Lansing

  6.) Kentucky       : [ ] Charleston        [ ] Cheyenne          [ ] Frankfort         [ ] Oklahoma City

  7.) Florida        : [ ] Trenton           [ ] Pierre            [ ] Tallahassee       [ ] Honolulu

  8.) Rhode Island   : [ ] Providence        [ ] Madison           [ ] Santa Fe          [ ] Trenton

  9.) Arkansas       : [ ] Boston            [ ] Little Rock       [ ] Harrisburg        [ ] Denver

 10.) Wisconsin      : [ ] Montgomery        [ ] Pierre            [ ] Madison           [ ] Richmond

capitalsquiz2_answers.txt`的内容:

Capitals Quiz #1

  1.) Oklahoma       : Oklahoma City
  2.) Virginia       : Richmond
  3.) North Carolina : Raleigh
  4.) Montana        : Helena
  5.) Alaska         : Juneau
  6.) Kentucky       : Frankfort
  7.) Florida        : Tallahassee
  8.) Rhode Island   : Providence
  9.) Arkansas       : Little Rock
 10.) Wisconsin      : Madison

推荐阅读