Functional requirements:
- Query the contact person, input the name, you can query the contact information in the current address book, if the contact person exists, output the contact information, if not, inform
- Insert a contact, and you can create a new contact in the address book. If the contact already exists, ask if you want to modify the contact information. If not, create a new contact
- Delete contact, you can delete contact, if the contact does not exist, inform
- Enter the command to exit the address book
Specific code implementation:
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # @Time : 2018/3/26 10:01 4 # @Author : yang 5 # @File : Exercise.py 6 # @Software: PyCharm 7 8 #Mail list 9 print('''|---Welcome to the address book---| 10 |---1,Query contact info ---| 11 |---2,Insert new contact---| 12 |---3,Delete contact information---| 13 |---4,Exit address book program---| 14 ''') 15 addressBook = {'Xiao Yang':12345678,'Xiao Zhang':12345679,'Xiao Zhao':12345670} 16 while 1: 17 order_code = input('Please enter the instruction code:') 18 if order_code.isdigit() == False: #Judge whether the instruction is only composed of numbers 19 print('The format of the instruction code you entered is wrong, please input again according to the prompt!') 20 continue 21 item = int(order_code) #Convert input instruction to integer 22 23 if item == 4: 24 print('Thank you for using the address book!') 25 break #End cycle 26 #Enter contact name 27 name = input('Please enter contact name:') 28 if item == 1: #Query contact info 29 if name in addressBook: 30 print(name,':',addressBook[name]) 31 continue #End current cycle 32 else: 33 print('The contact does not exist.') 34 if item == 2: #New contact 35 if name in addressBook: 36 print('The contact you entered already exists in the address book--->>',name,':',addressBook[name]) 37 is_Edit = input('Need to modify contact information(Y/N)') #Determine whether the current contact information needs to be modified 38 if is_Edit == 'Y': 39 userphone = input('Please enter the contact number:') 40 addressBook[name] = userphone 41 print(addressBook) 42 continue 43 else: 44 continue 45 else: 46 userphone = input('Please enter the contact number:') 47 addressBook[name]=userphone 48 print('Contact added successfully!') 49 print(addressBook) 50 continue 51 if item == 3: #Delete Contact 52 if name in addressBook: 53 del addressBook[name] 54 print('Contact deleted successfully!') 55 print(addressBook) 56 continue 57 else: 58 print('Contact does not exist')