After their own efforts to do a simple telephone shop input and query small procedures, relatively simple, like friends can practice practiced hand.
Topic:
Create your own command-line Address Book program. In this app, you can add, edit, delete, and search your contacts (friends, family, colleagues, etc.) and their information (such as e-mail addresses and/or phone numbers). These details should be saved for later extraction.
Knowledge points to use:
- Cpickle
- Simple file read/write
Code:
# coding=utf-8" "Name Age Email Query Modification" "Import Cpickle aspimport OSclassPersonObject): " "Staff Details" "def __init__ (self, name=none, Age=none, email=None): Self.name=name Self.age=Age Self.email=emaildef Show_all ():" "Print all People" "Persons=get_persons ()ifLen (Persons) >0: forIteminchPersons:print'Name:%s, age:%s, email:%s'%(Item.name, Item.age, Item.email)Else: Print"There is no person"def Add_person (name, age, email):" "Add People" "#判断是否为空 List=get_persons () with file ('Persons','W') asF:list.append (name, age, email) p.dump (list,f) def get_persons ():" "get a list of people" " ifNot os.path.exists ('Persons'): File ('Persons','W'). Close ()return[] with file ('Persons') asF:data=p.load (f)returnDataifData! = NoneElse[]def Find_person (name):" "Print information by name" "Persons=get_persons () forPersoninchpersons:ifPerson.name = =Name:print'Name:%s, age:%s, email:%s'%(Person.name,person.age,person.email)returnPrint'This no person named:name'def Del_person (name):" "Delete a person by name" "Persons=get_persons () forPersoninchpersons:ifPerson.name = =Name:persons.remove (person) with file ('Persons','W') asf:p.dump (persons,f) Print'Delete success!' returnPrint'This no person named:name'def main ():" "Show Welcome Statement" "Hello=" "Welcome Please input your purpose 1-See all person'S Detail 2-find person by name3-Add person4-Delete person5--Exit" " whiletrue:m= raw_input (Hello +'\n->') #print mifm = ='3': Name= Raw_input ("Name:") Age= Raw_input ("Age :") Email= Raw_input ("Email:") Add_person (name, age, email) print"ok!"elif M=='1': Show_all () elif m=='2': Name= Raw_input ("Enter The person ' s name:") Find_person (name) elif m=='4': Name= Raw_input ("Enter The person ' s name:") Del_person (name) elif m=='5': Os.sys.exit (0)if__name__ = ='__main__': Main ()
Python case One (phone shop)