• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python 电话本管理例子

python 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论
#!/usr/bin/env python#coding=utf-8#电话本管理,可以进行添加,删除,修改,查询用户信息import cPickle as p  class Telephone:    def __init__(self):        '''构造方法 '''             def addPeople(self,name,email,telephone):        """添加用户"""        teleDict = self.getDictData()        if teleDict:            infoList = [name,email,telephone]            teleDict[name] = infoList                 self.writefile(teleDict)        else:            teleDict = {}            infoList = [name,email,telephone]            teleDict[name] = infoList                   self.writefile(teleDict)                      def delPeople(self,name):        """删除用户"""        teleDict = self.getDictData()        if name in teleDict.keys():            del teleDict[name]            self.writefile(teleDict)        else:            print name,'is not in dict'         def editPeople(self,name,emial,telephone):        """ 修改信息"""        teleDict = self.getDictData()        if name in teleDict.keys():            infoList = [name,email,telephone]            teleDict[name] = infoList                 self.writefile(teleDict)            print name+'edit success'        else:            print name,'is not in dict'                  def getPeople(self,name):        """获取用户信息"""        teleDict = self.getDictData()        if teleDict:            if name in teleDict.keys():                people = teleDict[name]                print people        <mark style="color:transparent">来4源gaodaimacom搞#代%码*网</mark>    else:                print name,'is not in dict'        else:            print 'people is empty'                          def writefile(self, dictData):        """ 写入文件"""        f = file('dict.data','w')        p.dump(dictData,f)        f.close()             def getDictData(self):          """ 获取文件内容"""        fileName = 'dict.data'        try:            f = file(fileName)            teleDict = p.load(f)            return teleDict        except:            print 'open file error'   # 提示 信息def notice():    print "please enter 1-get people 2-add people 3-edit pelole 4-del people 5-get all people 0-break"     if __name__ == "__main__":    while(True):        notice()        userInput = int(raw_input())                 people = Telephone()        if userInput == 1:            name = raw_input("please enter user name:")            people.getPeople(name)        elif userInput == 2:            name = raw_input("enter name:")            email = raw_input("enter emai:")            telephone = raw_input("enter telephone:")            people.addPeople(name,email,telephone)        elif userInput == 3:            name = raw_input("enter name:")            email = raw_input("enter emai:")            telephone = raw_input("enter telephone:")            people.editPeople(name,email,telephone)        elif userInput == 4:            name = raw_input("enter del people name:")            people.delPeople(name)        elif userInput == 5:            allpeople = people.getDictData()            if allpeople:                for key in allpeople:                    print key,allpeople[key]            else:                print 'there is no people'        elif userInput == 0:            break        else:            print 'you select number is wrong'              raw_input('press enter')

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python 电话本管理例子
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址