python实践项目(七)

项目1:生成随机的测验试卷文件

假如你是一位地理老师,班上有35 名学生, 你希望进行美国各州首府的一个小测验。不妙的是,班里有几个坏蛋,你无法确信学生不会作弊。你希望随机调整问题的次序, 这样每份试卷都是独一无二的,这让任何人都不能从其他人那里抄袭答案。当然,手工完成这件事又费时又无聊。 好在,你懂一些Python
下面是程序所做的事:
创建35 份不同的测验试卷。
为每份试卷创建50 个多重选择题,次序随机。
为每个问题提供一个正确答案和3 个随机的错误答案,次序随机。
将测验试卷写到35 个文本文件中。
将答案写到35 个文本文件中。
这意味着代码需要做下面的事:
将州和它们的首府保存在一个字典中。
针对测验文本文件和答案文本文件,调用open()write()close()
利用random.shuffle()随机调整问题和多重选项的次序。 

#-*-coding:utf-8-*-

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

import random,os

testPath = 'C:\\Users\\Administrator\\Desktop\\Test'       # 试卷存放路径
answerPath = 'C:\\Users\\Administrator\\Desktop\\Answer'   # 答案存放路径

# 判断试卷及路径是否存在,若不存在,则创建对应路径
if not os.path.exists(testPath):
   os.makedirs(testPath)
if not os.path.exists(answerPath):
   os.makedirs(answerPath)

# The quiz data.Keys are states and values are their capitals.
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', 'WestVirginia':
            'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}

# Generate 35 quiz files.
for quizNum in range(35):
    # Create the quiz and answer key file.
    # os.path.join:将路径及文件名连接起来,相应的文件的位置随即确定。
    quizFile = open(os.path.join(testPath ,'capitalsquiz%s.txt' %(quizNum + 1)),'w')
    answerKeyFile = open(os.path.join(answerPath , '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 50 states,making a question for each.
    for questionNum in range(50):
        # 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 the 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'%('ABCD'[i],answerOptions[i]))
        quizFile.write('\n')

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


 //后续添加


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rs勿忘初心

您的鼓励将是我的最大创动原动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值