欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-11-26 15:34:42
核心代码: 复制代码 代码如下:#!/usr/bin/python#filename:friendbook.pyimport cpickle as pimport sys...

核心代码:

复制代码 代码如下:

#!/usr/bin/python
#filename:friendbook.py
import cpickle as p
import sys
import time
import 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 frilist
elif (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 frilist
elif (num=='6'):
        print "bye!"
else:
        print "please input the right number"

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