Functional Requirements:
- Query contact, enter the name, you can query the current contacts in the contact information, if the contact exists, the output contact information, if not exist, then inform
- Insert a contact, you can create a new contact in the contacts, if the contact already exists, ask whether to modify the contact information, if it does not exist, create a new contact
- Delete a contact, you can delete the contact, if the contact does not exist, tell
- Enter a command to exit contacts
Specific code implementation:
1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 #@Time: 2018/3/26 10:014 #@Author: Yang5 #@File: exercise.py6 #@Software: Pycharm7 8 #Contacts9 Print(" "|---Welcome to the Address Book---|Ten |---1, contact information---| One |---2, insert a new contact---| A |---3, delete contact information---| - |---4, exit Address Book Program---| - " ") theAddressBook = {'Xiao Yang': 12345678,'Xiao Zhang': 12345679,'Xiao Zhao': 12345670} - while1: -Order_code = input ('Please enter the instruction code:') - ifOrder_code.isdigit () = = False:#determine if the instruction is composed of only numbers + Print('The instruction code you entered is malformed, please re-enter it as prompted! ') - Continue +item = Int (Order_code)#converting an input instruction to an integral type A at ifitem = = 4: - Print('thanks for using the Address Book! ') - Break #End Loop - #Enter the name of the contact person -Name = input ('Please enter the name of the contact person:') - ifitem = = 1:#Query Contact Information in ifNameinchAddressBook: - Print(Name,':', Addressbook[name]) to Continue #End Current Loop + Else: - Print('The contact does not exist. ') the ifitem = = 2:#New contact Person * ifNameinchAddressBook: $ Print('the contact you entered already exists in the Address book--->>', Name,':', Addressbook[name])Panax NotoginsengIs_edit = input ('Whether you need to modify the contact information: (y/n)')#determine if you need to modify the current contact information - ifIs_edit = ='Y': theUserphone = input ('Please enter contact Phone:') +Addressbook[name] =Userphone A Print(AddressBook) the Continue + Else: - Continue $ Else: $Userphone = input ('Please enter contact Phone:') -addressbook[name]=Userphone - Print('Contact added successfully! ') the Print(AddressBook) - ContinueWuyi ifitem = = 3:#Delete a contact the ifNameinchAddressBook: - delAddressbook[name] Wu Print('Contacts deleted successfully! ') - Print(AddressBook) About Continue $ Else: - Print('contact does not exist.')
Python Learning-Dictionary exercises: simple Address Book