Python implements command line Address Book

Source: Internet
Author: User
This article describes how to write a small program in the command line address book. The following are the ideas and codes for writing. You are welcome to discuss them. 1. goals

Write a command line Address Book program to add, query, and delete address book friends and phone numbers.

2. implementation method

Creates a class to indicate a person's information. Use the dictionary to store the object of each person. The name is used as the key.
Use the pickle module to permanently store these objects.
Use the built-in dictionary method to add or delete the information of the modifier.

3. mind map

#1. create a dictionary to store address book information #2. the creator class contains three attributes: name, relationship, and phone number. #3. create operation class, including adding, querying, and deleting personnel, exit, save, and exit five methods #4. program running #5. determine whether the address book file exists #6. if yes, read the file to the personDictionary Dictionary #7. if it does not exist, prompt and create #8. while loop waiting for read command #9. if the command is addperson, add address book personnel #10. if the command is delperson, delete the contacts #11. if the command is search, find the address book personnel #12. if the command is quit, do not save and exit the program #13. if the command is sq, save the changes and exit the program.

5. write code based on Pseudocode

Import pickle as pimport OS #1. create a dictionary to store the address book information: personDictionary = {'name': {'relationship ': '', 'tel':''} relationshipList = ['parage', 'friends ', 'colleagues'] #2. the creator class contains three attributes: name, relation, and telephone. class Person: def _ init _ (self, name, relationship = relationshipList [1], tel = 'none '): personDictionary [name] = {'relationship ': relationship, 'tel': tel} #3. create Operation class, including ADD, query, and delete personnel, exit, save and exit five methods class Operation: def Addperson (): addname = input ('Enter name: ') addr Elationship = int (input ('select group (0: family, 1: friends, 2: colleagues): ') addtel = input ('Enter the phone number :') person (addname, relationshipList [addrelationship], addtel) def Delperson (): name = input ('Enter the name of the contact to be deleted: ') del personDictionary [name] def Search (): name = input ('Enter the name of the contact to be searched:') if name in personDictionary: print ('name: % s, link: % s, phone: % s' % (name, personDictionary [name] ['relationship '], personDictionary [name] ['Tel']) else: print ('contact does not exist. ') Def Quit (): running = False def SaveQuit (): f = open (addressbookFile, 'WB') p. dump (personDictionary, f) f. close () running = False #4. running = True #5. check whether the address book file contains addressbookFile = 'ssssbook. data' #6. if yes, read the file to the personDictionary dictionary if OS. path. exists (addressbookFile): f = open (addressbookFile, 'RB') personDictionary = p. load (f) #7. if it does not exist, the system prompts and creates else: jCommand = input ('The address book file is not found. Are you sure you want to create it? Yes/no ') if jCommand = 'yes': f = open (addressbookFile, 'WB') p. dump (personDictionary, f) f. close () elif jCommand = 'no': running = False #8. while loop waiting for reading command while running: command = input ('Enter command: ') #9. if the command is addperson, add the contacts if command = 'addperson ': Operation. addperson () continue #10. if the command is delperson, delete the contacts elif command = 'delimiter': Operation. delperson () continue #11. if the command is search, search for the address book personnel elif command = 'Search': Operation. search () continue #12. if the command is quit, do not save the exit program elif command = 'Quit': Operation. quit () break #13. if the command is sq, save the change and exit the program elif command = 'SQ ': Operation. saveQuit () break else: print ('no command found! ') Continue

6. Demo

Python3 addressbook. py enter Command: search enter the name of the contact to be searched: zhangsan contact does not exist. Enter command: addperson enter name: zhangsan select group (0: family, 1: friends, 2: colleagues): 1 enter Phone: 1234567 enter command: for search, enter the name of the contact to be searched: zhangsan name: zhangsan, link: friend, phone: 1234567. enter the command: sq $ Python3 addressbook. for py, enter the command: search, enter the name of the contact to be searched: zhangsan name: zhangsan, link: friend, phone: 1234567, enter the command: addperson, enter the name: lisi please select group (0: family, 1: friends, 2: colleagues): 1 please enter Phone: 1234567 please enter Command: q not found command! Enter command: quit $ Python3 addressbook. py enter Command: search enter the name of the contact to be searched: lisi contact does not exist. Enter command: search enter the name of the contact to be searched: zhangsan name: zhangsan, relationship: friend, tel: 1234567 enter Command: quit

For more articles about implementing the command line address book in Python, refer to PHP Chinese network!

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.