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

yipeiwu_com5年前Python基础

核心代码:

复制代码 代码如下:

#!/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,有诸多不足,以后改进

相关文章

python虚拟环境virtualenv的安装与使用

同一台服务器上部署多个项目时,项目可能使用不同版本的django或者其它不同的python库,这种情况下可以使用virtualenv来创建独立的python运行环境,将不同项目的运行环境...

zookeeper python接口实例详解

本文主要讲python支持zookeeper的接口库安装和使用。zk的python接口库有zkpython,还有kazoo,下面是zkpython,是基于zk的C库的python接口。...

flask中主动抛出异常及统一异常处理代码示例

flask中主动抛出异常及统一异常处理代码示例

本文主要介绍的是flask中主动抛出异常及统一异常处理的相关内容,具体如下。 在开发时,后台出现异常 ,但不想把异常显示给用户或者要统一处理异常时,可以使用abort主动抛出异常,再捕获...

使用pandas实现连续数据的离散化处理方式(分箱操作)

Python实现连续数据的离散化处理主要基于两个函数,pandas.cut和pandas.qcut,前者根据指定分界点对连续数据进行分箱处理,后者则可以根据指定箱子的数量对连续数据进行等...

pandas数值计算与排序方法

以下代码是基于python3.5.0编写的 import pandas food_info = pandas.read_csv("food_info.csv") # --------...