python实践项目(七)
来源:互联网 发布:腾讯内测手游软件 编辑:程序博客网 时间:2024/05/24 00:56
项目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,ostestPath = '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()
//后续添加
阅读全文
1 0
- python实践项目(七)
- 实践项目七:模拟银行转账系统(python+mysql)
- python实践项目(三)
- python实践项目(四)
- python实践项目(五)
- python实践项目(六)
- python实践项目(八)
- python实践项目(九)
- Python实践之(七)逻辑回归(Logistic Regression)
- python学习笔记(七)——小小的实践
- python实践项目(一)
- python实践项目(二)
- android实践项目七自定义的Spinner
- python计算机视觉项目实践
- python 文件读写项目实践
- 缺少python实践项目怎么办
- Maven实践(七)---仓库
- 实践项目四:桌面版python 查询天气(3)
- 数据结构第十一周项目3--图遍历算法实现
- 求两个数的最大公约数
- Java for Web学习笔记(九五):消息和集群(10)利用RabbitMQ实现订阅和发布
- loop设备的删除
- JAVA第六次作业
- python实践项目(七)
- Short Program CodeForces
- JS中innerHTML outerHTML innerText outerText 和 value
- 5.2
- python 迭代器、生成器基础知识
- python笔记---需求文件requirements.txt的创建及使用
- 线程监控
- 集体智慧编程中文版错误记录
- iOS Reactivecocoa(RAC)原理分析