题目:
有一个单词表以字符串形式表达如下:
words = ”’
#-blue-蓝色(的)
#-green-绿色(的)
#-red-红色(的)
#-yellow-黄色(的)
#-orange-橘色(的)
#-purple-紫色(的)
#-white-白色(的)
#-black-黑色(的)
#-brown-棕色(的)
”’
(1)请写一个程序,在程序运行时给用户中文提示,要求用户拼写出英文,根据>用户的拼写是否正确,确定进行下一个单词的拼写或重新拼写。
(2)修改第1题,设计一个字典,能够记录拼错的单词和该单词拼错的次数。
(3)在1、2题的基础上使之能够保存拼错的单词列表,并按照要求生成新的拼错单词文件,例如:输出累计拼错3次以上的单词列表。
提示:将单词表读入系统的操作由文件读写函数完成。这时,words在系统中是一个字符串列表,>遍历列表可以得到形如“#-red-红色(的)”的字符串,这时就可利用字符串分割得到中文和英文了,然后就用if判断解决问题好了。一个具体的甚至>复杂应用大都是可以用简单的技术组合解决。
分析:
首先定义字符串words:
words = '''
#-blue-蓝色(的)
#-green-绿色(的)
#-red-红色(的)
#-yellow-黄色(的)
#-orange-橘色(的)
#-purple-紫色(的)
#-white-白色(的)
#-black-黑色(的)
#-brown-棕色(的)
'''
定义元祖存放菜单:
menu_tuple = ("单词记忆系统(Python版)", "\t1:背单词", "\t2:常错单词列表", "\t3:导出单词列表", "\t0:退出系统")
创建字典,用来存放拼写错误的单词和次数:
words_dict = {}
while循环,遍历元祖,循环显示菜单,注意使用try/except进行异常处理。
while choice != 0:
for i in menu_tuple: # 遍历元祖,显示目录
print i
try:
choice = input("请选择功能:")
except IOError:
print "对不起,输入错误!"
else:
if choice == 1:
print "进入背单词模式"
recite_words(words, words_dict)
elif choice == 2:
print "常错单词列表"
error_words(words_dict)
elif choice == 3:
print "导出单词列表"
print_list(words, words_dict)
elif choice == 0:
print "成功退出系统!"
break
else:
print "输入错误,请重新输入!"
背单词模块函数:
def recite_words(words, words_dict):
times = 0 # 初始化计数器
print "请根据中文写出英文:"
words = words.split('#')
del words[0]
for i in words:
r = i.split('-')
while 1: # 应该是最近没有怎么写代码,写到调出循环重复输入就卡住了,万能的while-break用法啊
print (r[-1]).strip()
try:
eninput = raw_input("请输入对应的英文:")
except IOError:
print "对不起,输入错误!"
else:
if cmp(eninput, r[-2]): # 使用cmp函数判断输入是否正确,正确返回0
print "对不起,英文错误,请重新拼写!"
times += 1 # 次数加1
words_dict_cp = {r[-2]: `times`}
words_dict.update(words_dict_cp)
else:
print "回答正确!继续下一个单词。"
times = 0 # 重置计数器
break # 跳出循环
常错单词列表模块函数:
def error_words(words_dict):
table = PrettyTable()
table.field_names = ["常错单词", "累计错误次数"]
for i in words_dict.items():
table.add_row(i[:])
print table
按要求导出单词列表模块函数:
def print_list(words, words_dict):
words_dict_new = {} # 错误次数筛选字典
wordsdist = {} # 字符串转存字典
words_print = {} # 筛选单词导出字典
try:
input = raw_input("请输入要求生成文件的单词累计错误次数:")
except IOError:
print "对不起,输入错误!"
else:
# 在单词错误次数统计字典中查询是否有匹配的次数
for (key, value) in words_dict.items():
if input in value:
words_dict_new[key] = value
# 存在查询的单词错误次数,列表输出并导出文件
if words_dict_new != {}:
table = PrettyTable()
table.field_names = ["常错单词", "累计错误次数"]
for i in words_dict_new.items():
table.add_row(i[:])
print table
# 将字符串转换成字典,便于操作
words = words.split("#") # 以"#"切分字符串
del words[0] # 删除开头无用的"\n\t"字段
for a in words:
b = a.split("-") # 以"-"切分字符串
del b[0] # 删除开头无用的"\n\t"字段
wordsdist[(b[0]).strip()] = (b[-1]).strip() # 将数据以键-值对的方式存入字典
# 从字典wodsdist中筛选符合条件的键,存入字典words_print中
for (key, value) in wordsdist.items():
if key in words_dict_new.keys():
words_print[key] = value
# 打开文件"test.txt"并以追加的方式从字典中写入值
f = open("test.txt", "a")
try: # 异常处理
for j in words_print.items():
f.writelines(j[0])
f.writelines(" ")
f.writelines(j[-1])
f.writelines("\n")
except IOError:
print "对比起,输入错误!"
finally:
f.closed # 关闭文件
print "文件导出成功,导出文件名为:test.txt"
else:
print "对不起,没有累计错误次数为" + input + "的单词!"