代码还是有bug
效果:
**欢迎使用光荣之路单词小字典**
1、增加单词
2、查找单词
3、删除单词
4、退出单词
请选择序列号 *
*****欢迎下次再次使用**
eter:
===============
# -*- coding:utf-8 -*-
import pickle
WordData = {}
'''菜单兰'''
def WordMenu():
print u'\t\t\t****欢迎使用光荣之路单词小字典***\t\t\t'
print u'\t\t\t*** 1、增加单词 ***\t\t\t'
print u'\t\t\t*** 2、查找单词 ***\t\t\t'
print u'\t\t\t*** 3、删除单词 ***\t\t\t'
print u'\t\t\t*** 4、退出单词 ***\t\t\t'
print u'\t\t\t*** 请选择序列号 ***\t\t\t'
print u'\t\t\t*******欢迎下次再次使用**********\t\t\t'
def addWord():
try:
yesno = raw_input('Please enter yes and no sure whether to increase the words:')
if yesno == 'no':
return
while True:
word = raw_input("please input add word:")
word_meaning = raw_input("Please input word meaning:")
wr = file("wordData.kpl",'rb')
WordData = pickle.load(wr)
if WordData.has_key(word):
print 'The %s word already exists, do not increase again'%word
wr.close()
continue
ww = file("wordData.kpl",'wb')
WordData[word] = word_meaning
pickle.dump(WordData,ww)
print 'Words updated, thank you for maintenance'
ww.close()
break
except:
print 'Please first initialize storage file system!!!\n'
def delWord():
try:
while True:
word = raw_input("please input your word to delete:")
fr = file("wordData.kpl",'rb')
WordData = pickle.load(fr)
if WordData.has_key(word):
print 'You want to delete the word is:',WordData[word]
del WordData[word]
print 'Deleted successfully!, please use again!!'
fr.close()
continue
else:
print 'You want to delete the word does not exist\n'
break
except:
print 'Input is wrong\n'
def FindWord():
try:
while True:
word = raw_input("Please enter to find words:")
fr = file("wordData.kpl",'rb')
WordData = pickle.load(fr)
if WordData.has_key(word):
print 'You find the word is: %s, the meaning is: %s'%(word,WordData[word])
continue
elif word == 'exit':
break
else:
print 'word is not find!'
except:
print 'Welcome to use the menu'
#初始化
def MenuInitialize():
WordData = {'liwen': "李玟"}
fw = open('wordData.kpl', 'wb')
# Pickle the list using the highest protocol available.
pickle.dump(WordData, fw, -1)
print u'初始化成功!!'
def MenuMain():
try:
while True:
WordMenu()
num = int(raw_input('eter:\n'))
if num == 1:
addWord()
continue
elif num == 2:
FindWord()
continue
elif num == 3:
delWord()
continue
else:
print u"欢迎再次使用"
break
except:
print "Your input is wrong"
if __name__ == "__main__":
MenuMain() #入口
#MenuInitialize() #字典初始化
本文转自 知止内明 51CTO博客,原文链接:http://blog.51cto.com/357712148/2049159,如需转载请自行联系原作者