Python Phone Book Management example

Source: Internet
Author: User
#!/usr/bin/env python#coding=utf-8# Phone Book management, you can add, delete, modify, query user information import Cpickle as P class Telephone:def __init__ (self): "Construction Method" Def addpeople (Self,name,email,telephone): "" "Add User" "" Teledict = Self.getdictda                 Ta () if teledict:infolist = [Name,email,telephone] teledict[name] = infolist Self.writefile (teledict) else:teledict = {} infolist = [Name,email,telephone] Te Ledict[name] = infolist Self.writefile (teledict) def delpeople (self,name): "            "" Delete User "" "Teledict = Self.getdictdata () If name in Teledict.keys (): Del Teledict[name] Self.writefile (teledict) else:print name, ' isn't in Dict ' Def editpeople (Self,name,emial,telep Hone): "" "" "" "" "" "" Teledict = Self.getdictdata () If name in Teledict.keys (): Infolist = [n    Ame,email,telephone]        Teledict[name] = infolist self.writefile (teledict) print name+ ' edit Success ' Els  E:print name, ' isn't in Dict ' Def getPeople (self,name): "" "Get User Information" "" Teledict =                Self.getdictdata () If teledict:if name in Teledict.keys (): people = Teledict[name] Print people else:print name, ' isn't in dict ' else:print ' people        is empty ' def writefile (self, Dictdata): "" "" "Write File" "" F = [File (' Dict.data ', ' W ') P.dump (dictdata,f) f.close () def getdictdata (self): "" "Get File Contents" "" FileName = ' dict.d            ATA ' try:f = file (fileName) teledict = P.load (f) return teledict except: print ' Open file error ' # hint message 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:") PEO Ple.getpeople (name) elif Userinput = = 2:name = Raw_input ("Enter name:") email = raw_input ("E        Nter emai: ") telephone = raw_input (" Enter Telephone: ") people.addpeople (Name,email,telephone) elif Userinput = = 3:name = Raw_input ("Enter name:") email = raw_input ("Enter Emai:") te            Lephone = 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 ')
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.