Python implementation command line Address Book instance tutorial, python instance tutorial
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
4. Write pseudocode
#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
Summary
The above is all the content of the Python command line Address Book instance tutorial. If you have any questions, leave a message to discuss it. I hope this article will help you learn how to use python.