8.5 项目:生成随机的测验试卷文件
项目要求:假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验。不妙的是,班里有几个坏蛋,你无法确信学生不会作弊。你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人都不能从其他人那里抄袭答案。
import os
os.chdir(os.path.join(os.getcwd(),'new'))
import random
# import quiz data
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 35 quiz files.
for quizNum in range(1,36):
# TODO: Create the quiz and answer key files.
quizfile = open('quizfile%02d.txt' % (quizNum),'w')
quizans = open('quiznum%02d.txt' % (quizNum),'w')
# TODO: Write out the header for the quiz.
quizfile.write('Name:\n\nDate:\n\n')
quizfile.write(('State Capital Quiz form-%02d' % (quizNum)).rjust(50))
quizfile.write('\n\n')
quizans.write('Answers for quiz form-%02d:\n' % (quizNum))
# TODO: Shuffle the order of the states.
state_list = list(capitals.keys())
random.shuffle(state_list)
# TODO: Loop through all 50 states, making a question for each.
for questionNum in range(len(capitals)):
quizfile.write("%d. What's the capital of %s?\n" % (questionNum+1,
state_list[questionNum]))
# Write the question and the answer options to the quiz file.
ans_sheet = []
wrong_ans = list(capitals.values())
wrong_ans.remove(capitals[state_list[questionNum]])
ans_sheet = random.sample(wrong_ans, 3)
ans_sheet.append(capitals[state_list[questionNum]])
random.shuffle(ans_sheet)
for i in range(4):
quizfile.write(" %s. %s\n" % ('ABCD'[i], ans_sheet[i]))
quizfile.write('\n')
# Write the answer key to a file.
quizans.write('%d. %s(%s)\n' % (questionNum+1,
'ABCD'[ans_sheet.index(capitals[state_list[questionNum]])],
capitals[state_list[questionNum]]))
quizfile.close()
quizans.close()
跟着这个项目解答练手需要耐心,因为代码有点长,更加需要突破自己的惰性和心理障碍去一步步执行。作者这个项目讲解很细致。
8.6 项目:多重剪贴板
项目要求:程序将利用一个关键字保存每段剪贴板文本。例如,当运行 py mcb.pyw save spam,剪贴板中当前的内容就用关键字 spam 保存。通过运行 py mcb.pyw spam,这段文本稍后将重新加载到剪贴板中。如果用户忘记了都有哪些关键字,他们可以运行 py mcb.pyw list,将所有关键字的列表复制到剪贴板中。
#! python3
import shelve, pyperclip, sys
mcbshelf = shelve.open('mcb')
mcbshelf['name'] = 'wuwuwuwuwu'
mcbshelf['address'] = 'xiangshanli co. Tl'
if len(sys.argv) == 3 and sys.argv[1].lower == 'save':
mcbshelf[sys.argv[2]] = pyperclip.paste()
elif len(sys.argv) == 2:
if sys.argv[1].lower() == 'list':
pyperclip.copy(str(list(mcbshelf.keys())))
elif sys.argv[1] in mcbshelf:
pyperclip.copy(mcbshelf[sys.argv[1]])
print(mcbshelf['name'])
mcbshelf.close()
思路:这里需要理解命令行参数,通过cmd执行语句
8.9.1 实践项目:扩展多重剪贴板
项目要求:扩展本章中的多重剪贴板程序,增加一个 delete <keyword>命令行参数,它将从 shelf 中删除一个关键字。然后添加一个 delete 命令行参数,它将删除所有关键字。
#! python3
import shelve, pyperclip, sys
mcbshelf = shelve.open('mcb')
if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':
mcbshelf[sys.argv[2]] = pyperclip.paste()
elif len(sys.argv) == 3 and sys.argv[1].lower() == 'delete':
try:
del mcbshelf[sys.argv[2]]
except KeyError:
print('there is no key you just entered, please check <pytest.pyw list>')
elif len(sys.argv) == 2:
if sys.argv[1].lower() == 'list':
pyperclip.copy(str(list(mcbshelf.keys())))
elif sys.argv[1] in mcbshelf:
pyperclip.copy(mcbshelf[sys.argv[1]])
elif sys.argv[1].lower() == 'delete':
mcbshelf.clear()
print('all data have been delete!')
mcbshelf.close()
思路:
- 理解了项目8.6后,加个delete语句就不难了
- 注意加个try-except进行测试
8.9.2 实践项目:疯狂填词
#! python3
# this is program named 'Mad Libs'
"""
# this is the original txt
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
unaffected by these events.
# input() GUI
Enter an adjective:
silly
Enter a noun:
chandelier
Enter a verb:
screamed
Enter a noun:
pickup truck
# output a new txt like this
The silly panda walked to the chandelier and then screamed. A nearby pickup
truck was unaffected by these events.
"""
# read the original txt
filename = 'o_madlibs.txt'
with open(filename,'r') as o_ml:
o_text = o_ml.readline()
# give an input()
ADJ = input('Enter an adjective:\n')
NOU1 = input('Enter a noun:\n')
VER = input('Enter a verb\n')
NOU2 = input('Enter a noun:\n')
# write out a new txt , whose words haved been replaced
o_text = o_text.replace('ADJECTIVE',ADJ)
o_text = o_text.replace('NOUN',NOU1,1)
o_text = o_text.replace('VERB', VER)
o_text = o_text.replace('NOUN',NOU2,1)
print(o_text)
with open('new_libs.txt','w') as f:
f.write(o_text)
思路:
- 想了下,根本就没必要用正则啊!
- 所以就投机取巧用replace代替了哈哈哈,这才符合python之禅
8.9.3 正则表达式查找
项目要求:编写一个程序,打开文件夹中所有的.txt 文件,查找匹配用户提供的正则表达式的所有行。结果应该打印到屏幕上。
#! python3
# this is program named 'Regex Search TXT'
"""
编写一个程序,打开文件夹中所有的.txt 文件,查找匹配用户提供的正则表达
式的所有行。结果应该打印到屏幕上
"""
import re, os
# 编写一个正则表达式,匹配txt中所有小写5个字母的单词
five_word = re.compile(r'\s[a-z]{5}\s')
# 打开指定文件夹的所有txt文件,匹配所有行
re_ans = []
pathname = r'c:\users\hp\desktop'
for filename in os.listdir(pathname):
if filename.endswith('.txt'):
with open(filename, 'r') as f:
t_text = f.read()
t_re_find = five_word.findall(t_text)
re_ans += t_re_find
print(re_ans)
思路:
- 用了个简单的正则(找5个小写字母的单词)
- 配合os.listdir,找出txt文件,并for循环一一打开并匹配正则
环境:python3
想做这个系列文章,就是因为当时看这本书时,想看看网上有没更优美的解决,但是略难找到。所以就把自己的项目练习放在了一个txt文件中,现在把练习代码放到这里,有不足之处希望大家能给出指导意见及相互交流、提升。