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


Posted in Python onNovember 29, 2013

核心代码:

#!/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 相关文章推荐
Python 元类使用说明
Dec 18 Python
Python中的exec、eval使用实例
Sep 23 Python
Python实现程序的单一实例用法分析
Jun 03 Python
Python操作RabbitMQ服务器实现消息队列的路由功能
Jun 29 Python
pip安装py_zipkin时提示的SSL问题对应
Dec 29 Python
python用quad、dblquad实现一维二维积分的实例详解
Nov 20 Python
python、PyTorch图像读取与numpy转换实例
Jan 13 Python
Python爬虫程序架构和运行流程原理解析
Mar 09 Python
Python代码注释规范代码实例解析
Aug 14 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
Jan 27 Python
Python+Appium新手教程
Apr 17 Python
Matplotlib绘制条形图的方法你知道吗
Mar 21 Python
Python时间戳与时间字符串互相转换实例代码
Nov 28 #Python
python计算程序开始到程序结束的运行时间和程序运行的CPU时间
Nov 28 #Python
SublimeText 2编译python出错的解决方法(The system cannot find the file specified)
Nov 27 #Python
Pyramid添加Middleware的方法实例
Nov 27 #Python
linux环境下安装pyramid和新建项目的步骤
Nov 27 #Python
Pyramid将models.py文件的内容分布到多个文件的方法
Nov 27 #Python
Pyramid Mako模板引入helper对象的步骤方法
Nov 27 #Python
You might like
php可应用于面包屑导航的递归寻找家谱树实现方法
2015/02/02 PHP
php基础设计模式大全(注册树模式、工厂模式、单列模式)
2015/08/31 PHP
php生成带logo二维码方法小结
2016/04/08 PHP
Zend Framework上传文件重命名的实现方法
2016/11/25 PHP
JS setCapture 区域外事件捕捉
2010/03/18 Javascript
基于jquery ajax 用户无刷新登录方法详解
2012/04/28 Javascript
JS画线(实例代码)
2013/11/20 Javascript
JavaScript修改css样式style动态改变元素样式
2013/12/16 Javascript
JS实现DIV容器赋值的方法
2015/12/14 Javascript
详解JavaScript实现设计模式中的适配器模式的方法
2016/05/18 Javascript
微信小程序实战之自定义模态弹窗(8)
2017/04/18 Javascript
Vue自定义图片懒加载指令v-lazyload详解
2020/12/31 Javascript
vue双花括号的使用方法 附练习题
2017/11/07 Javascript
详解vuex状态管理模式
2018/11/01 Javascript
JavaScript 判断数据类型的4种方法
2020/09/11 Javascript
[04:04]显微镜下的DOTA2第六期——电影级别的华丽团战
2014/06/20 DOTA
[02:22:36]《加油!DOTA》总决赛
2014/09/19 DOTA
Python中replace方法实例分析
2014/08/20 Python
在Python程序中进行文件读取和写入操作的教程
2015/04/28 Python
Python学习思维导图(必看篇)
2017/06/26 Python
Python操作Sql Server 2008数据库的方法详解
2018/05/17 Python
python3 selenium 切换窗口的几种方法小结
2018/05/21 Python
python使用正则筛选信用卡
2019/01/27 Python
简单了解python中的与或非运算
2019/09/18 Python
python 图像的离散傅立叶变换实例
2020/01/02 Python
python实现猜单词游戏
2020/05/22 Python
html5 横向滑动导航栏的方法示例
2020/05/08 HTML / CSS
全球知名旅游社区巴西站点:TripAdvisor巴西
2016/07/21 全球购物
小学毕业典礼演讲稿
2014/09/09 职场文书
民政局副局长民主生活会个人整改措施
2014/10/04 职场文书
终止或解除劳动合同及劳动关系的证明书
2014/10/06 职场文书
旷课检讨书范文
2014/10/30 职场文书
幼儿教师师德师风自我评价
2015/03/05 职场文书
论文答辩开场白大全
2015/05/27 职场文书
【2·13】一图读懂中国无线电发展
2022/02/18 无线电
如何用H5实现好玩的2048小游戏
2022/07/23 HTML / CSS