Python case One (phone shop)

Source: Internet
Author: User

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:
    1. Cpickle
    2. 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)

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.