一个简单的python程序实例(通讯录)

核心代码:

代码如下:

#!/usr/bin/python#filename:friendbook.pyimport cpickle as pimport sysimport timeimport os

ab={‘xdex’:’cneds@fnedf.com’, ‘laexly’:’fev@fe.com’, ‘fukc’:’fexok@ver.com’, ‘stifu’:’stif@qq.com’}

def dumpfile(list): f=file(friendab,’w’) p.dump(list,f) f.close()

if os.path.isfile(‘friendab.data’): friendab=’friendab.data’else: os.touch(‘friendab.data’) dumpfile(ab) del ab

f=file(friendab)frilist=p.load(f)

class person: def __init__(self,name): self.name=name def saysome(self): print ‘the friend %s,his e-mail is %s ‘%(sname,frilist[sname])class addperson: def __init__(self,name,email): self.name=name self.email=email def addbook(self): ab=frilist ab[sname]=email dumpfile(ab) del ab print ‘succlessful!’class delperson: def __init__(self,name): self.name=name def delbook(self): ab=frilist ab.pop(sname) dumpfile(ab) del ab print ‘success del’

class alterperson: def __init__(self,name,email): self.name=name self.email=email def alterbook(self): ab=frilist ab[sname]=email dumpfile(ab) del ab print ‘succlessful update!’

print ”’\this program prints files to the standard output.any number of files can be specified.options include:[1] : search your friend’s email from friendsbook[2] : add your friend’s email to firendsbook[3] : del your friend’s email from firnedsbook[4] : alter your friend’s email from friendsbook[5] : all friends list[6] : exit the program”’

num=raw_input(‘press the number [1,2,3,4,5] –>’)

if (num==’1′): sname=raw_input(‘enter the name–>’) if sname in frilist: p=person(sname) p.saysome() else: print ‘not in it’elif (num==’2’): sname=raw_input(‘enter the name–>’) email=raw_input(‘enter the email–>’) pa=addperson(sname,email) pa.addbook() #p=person(sname) #p.saysome() print frilistelif (num==’3′): sname=raw_input(‘enter the name–>’) pa=delperson(sname) pa.delbook()elif (num==’4′): sname=raw_input(‘enter the name–>’) if sname in frilist: email=raw_input(‘enter the email–>’) p=alterperson(sname,email) p.alterbook() else: print ‘not in it’elif (num==’5′): print frilistelif (num==’6’): print “bye!”else: print “please input the right number”

注:这是本人写的第一个python,有诸多不足,以后改进

Posted in 未分类

发表评论