1. 程式人生 > >python3.6 字典 自動生成生成不同習題和答案不同的試卷 練習記錄

python3.6 字典 自動生成生成不同習題和答案不同的試卷 練習記錄

import random

import time

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', 'NewMexico':
                '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'}


def outputQuiz(capitals):
    '''
    :param capitals: 
    :return: 
    '''
    for quizNum in range(10):
        # Create quizfile and answerfile
        quizFile = open('capitals_quiz_{0}.txt'.format(quizNum + 1), 'w')
        answerFile = open('capitals_answer_{0}.txt'.format(quizNum + 1), 'w')
        quizFile.write(time.strftime("%Y年%m月%d日 製作", time.localtime()))
        quizFile.write('\n\nName:\n\nDate:\n\nPeriod:\n\n')
        quizFile.write('Capital from capitals_quiz_{0}.txt'.format(quizNum + 1).center(50, '*'))
        quizFile.write('\n\n')
        states = list(capitals.keys())
        for questionsNUm in range(len(states)):
            correctAnswer = capitals[states[questionsNUm]]
            wrongAnswers = list(capitals.values())
            del wrongAnswers[wrongAnswers.index(correctAnswer)]
            wrongAnswers = random.sample(wrongAnswers, 3)
            answetOption = wrongAnswers + [correctAnswer]
            random.shuffle(answetOption)
            quizFile.write('{0}. What is the capital of the {1}\n\n'.format(questionsNUm + 1, states[questionsNUm]))
            option = 'ABCD'
            for i in range(len(option)):
                quizFile.write('{0}. {1}\n'.format(option[i], answetOption[i]))
            quizFile.write('\n')
            answerFile.write('{0}. {1} \n\n'.format(questionsNUm + 1, option[answetOption.index(correctAnswer)]))
        quizFile.write('Done'.center(50, '*'))
        quizFile.close()
        answerFile.close()
    return 'Done'


print(outputQuiz(capitals))

2018年05月15日 製作

Name:

Date:

Period:

****Capital from capitals_quiz_6.txt****

  1. What is the capital of the Alabama

A. Richmond
B. Providence
C. Columbia
D. Montgomery

  1. What is the capital of the Alaska

A. Springfield
B. Juneau
C. Columbia
D. Denver

  1. What is the capital of the Arizona

A. Phoenix
B. Montpelier
C. Austin
D. Springfield
××××skip×××××××
Capital from capitals_quiz_6.txt answers


1. B

  1. A

  2. C