python中pickle使用学习-字典小程序函数化

代码还是有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() #字典初始化

转载于:https://blog.51cto.com/357712148/2049159

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值