Python phone book management example

Source: Internet
Author: User
Python phone book management example
#! /Usr/bin/env python # coding = UTF-8 # manage the phone book. you can add, delete, modify, and query user information. import cPickle as p class Telephone: def _ init _ (self): ''' constructor ''' def addPeople (self, name, email, telephone ): "add user" 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): "delete user" 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): "" modify information "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 ): "get user information" "teleDict = self. getDictData () if teleDict: if name in teleDict. keys (): people = teleDict [name] print people else: print name, 'is not 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 content" "fileName = 'dict. data 'try: f = file (fileName) teleDict = p. load (f) return teleDict failed t: print 'open file error' # 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 :") 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 ')
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.