after reading some basic Python tutorials, I felt a deep sense of the simplicity and power of Python, which was my first Python Demo. Here is the complete code with execution. Code:
# encoding:utf-8 ' @author: techzero@email: [email protected] @time: 2014-4-30 pm 1:31:04 ' Import Osimport Sysimport Cpickle as Pclass person:def __init__ (self, Name, email): "Initializes the person ' s data." Self.name = Name Self.email = emaildef Create (): "" "Create new person and input email" "Global Persons Try:name = raw_input ("Please input name:") while Persons.has_key (name): Name = Raw_input ("This n Ame have already exist, please input new name: ") e-mail = raw_input (" Please input email: ") except Eoferror: print ' \neof Error ' sys.exit () persons[name] = email print "def delete ():" "Search person by name and D Elete "" "Global Persons Try:name = raw_input (" Please input the person ' s name you want to delete: ") except Eoferror:print ' \neof Error ' sys.exit () if Persons.has_key (name): Del Persons[name] Save ( ) Else:print "No one calLed ", name,"!\n "def Modify ():" "" Search person by name and modify email "" "Global Persons Try:name = raw_i Nput ("Please input the person ' s name want to modify:") if Persons.has_key (name): Del Persons[name] email = raw_input ("Please input new email:") persons[name] = email Save () Else: Print "No one called", Name, "!\n" except Eoferror:print ' \neof Error ' Sys.exit () def Save (): "" "Save Persons to File" "" Global Persons file = ' person.dat ' F = file (file, ' W ') P.dump (Persons, f) f.close ( ) print "Operation Done!\n" Def read (): "" "" "" "" Read from File "" "Global Persons file = ' Person.dat ' if OS . path.exists (file): F = file (file) Persons = P.load (f) f.close () Else:file = ' Person.dat ' f = file (file, ' W ') F.close () def display (): "" "Display all persons in the dictionary" "" Global persons For name, e-mail in PErsons.items (): Print "", name,email print "Def search ():" "Search Person by name" "" Global Perso NS Try:name = raw_input ("Please input the person ' s name," want to Search: ") except Eoferror:print ' \neof Error ' sys.exit () if Persons.has_key (name): print "", name,persons[name], "\ n" Else: Print "No one called", Name, "!\n" def menu (): "" "Display a menu to choose Operation" "" Choose = "0" while True: #i = Os.system ("CLS") print ' 1----Create2----Delete3----MODIFY4----SEARCH5----Display6----Exit ' tr Y:choose = Raw_input ("Choose an item (1-6):") except Eoferror:print ' \neof Error ' Sys.exit () If choose = = "1": Create () elif Choose = = "2": Delete () elif Choose = = "3": Modify () elif Choose = = "4": Search () elif Choose = = "5": D Isplay () elif ChoOSE = = "6": print "Thanks for using!" Sys.exit () else:print "" Persons = {}read () menu ()
Perform
This article fixed link: http://www.itechzero.com/coding/python/python-development-with-eclipse-pydev-install-tutorial/, reprint please indicate the source.