python中pickle使用学习-字典小程序函数化
2017-12-10 18:46
507 查看
代码还是有bug
效果:
**欢迎使用光荣之路单词小字典**
1、增加单词 2、查找单词 3、删除单词 4、退出单词 请选择序列号 *
*****欢迎下次再次使用[b]**[/b]
eter:
===============
效果:
**欢迎使用光荣之路单词小字典**
1、增加单词 2、查找单词 3、删除单词 4、退出单词 请选择序列号 *
*****欢迎下次再次使用[b]**[/b]
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() #字典初始化
相关文章推荐
- python中pickle使用学习-字典小程序学习
- python学习笔记三--字典的使用
- 木其工作室(专业程序代写服务)[转]每个程序员都应该学习使用Python或Ruby
- 树莓派学习系列5——使用python编写简单的游戏程序
- Python 2.7 学习笔记 字典(map)的使用
- python学习笔记2-字典的使用
- Python学习之使用ctypes模块操作C扩展程序
- python学习笔记:字典的使用示例详解
- Python学习_我该怎么使用字典
- 重新学习python线程应用使用thread类,改写程序
- python学习笔记28(python中pickle的使用)
- [学习笔记]使用py2exe打包python程序
- Python学习_我该怎么使用字典的嵌套功能
- 神经网络与深度学习 1.6 使用Python实现基于梯度下降算法的神经网络和MNIST数据集的手写数字分类程序
- python 学习笔记 11 -- 使用参数使你的程序变得更性感
- 【python入门】学习字典的随笔,附使用字典做一个学生管理系统
- Python学习9:字典dict的使用
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- python学习之创建和使用字典
- python 列表 字典 读写文件:pickle模块的基本使用