一个简单的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 相关文章推荐
Pyramid添加Middleware的方法实例
Nov 27 Python
python模拟登录百度代码分享(获取百度贴吧等级)
Dec 27 Python
探究python中open函数的使用
Mar 01 Python
Python爬取网易云音乐热门评论
Mar 31 Python
python实现超简单的视频对象提取功能
Jun 04 Python
python中plot实现即时数据动态显示方法
Jun 22 Python
对numpy Array [: ,] 的取值方法详解
Jul 02 Python
python实现控制台打印的方法
Jan 12 Python
500行代码使用python写个微信小游戏飞机大战游戏
Oct 16 Python
TensorFlow tf.nn.max_pool实现池化操作方式
Jan 04 Python
如何解决cmd运行python提示不是内部命令
Jul 01 Python
详解selenium + chromedriver 被反爬的解决方法
Oct 28 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和XSL stylesheets转换XML文档
2006/10/09 PHP
PHP对象相互引用的内存溢出实例分析
2014/08/28 PHP
php实现refresh刷新页面批量导入数据的方法
2014/12/23 PHP
一张表搞清楚php is_null、empty、isset的区别
2015/07/07 PHP
Yii2实现UploadedFile上传文件示例
2017/02/15 PHP
php模拟post提交请求调用接口示例解析
2020/08/07 PHP
javascript 触发HTML元素绑定的函数
2010/09/11 Javascript
jQuery+CSS 半开折叠效果原理及代码(自写)
2013/03/04 Javascript
jQuery对Select的操作大集合(收藏)
2013/12/28 Javascript
表单提交前触发函数返回true表单才会提交
2014/03/11 Javascript
js Dialog 去掉右上角的X关闭功能
2014/04/23 Javascript
JavaScript组合拼接字符串的效率对比测试
2014/11/06 Javascript
js实现漂浮回顶部按钮实例
2015/05/06 Javascript
mac下的nodejs环境安装的步骤
2017/05/24 NodeJs
从零开始封装自己的自定义Vue组件
2018/10/09 Javascript
解决vue 界面在苹果手机上滑动点击事件等卡顿问题
2018/11/27 Javascript
详解Vue-cli3 项目在安卓低版本系统和IE上白屏问题解决
2019/04/14 Javascript
在Angular中实现一个级联效果的下拉框的示例代码
2020/05/20 Javascript
vue-router 按需加载 component: () => import() 报错的解决
2020/09/22 Javascript
[06:30]DOTA2英雄梦之声_第15期_死亡先知
2014/06/21 DOTA
Python socket C/S结构的聊天室应用实现
2014/11/30 Python
python九九乘法表的实例
2017/09/26 Python
python读取图片并修改格式与大小的方法
2018/07/24 Python
python3实现mysql导出excel的方法
2019/07/31 Python
如何表示python中的相对路径
2020/07/08 Python
Python通过类的组合模拟街道红绿灯
2020/09/16 Python
幼儿园庆六一活动方案
2014/03/06 职场文书
公司门卫岗位职责
2014/03/15 职场文书
初一学生期末评语
2014/04/24 职场文书
党的群众路线教育实践活动个人对照检查材料范文
2014/09/25 职场文书
个人批评与自我批评材料
2014/10/17 职场文书
化工厂员工工作总结
2015/10/15 职场文书
使用Springboot实现健身房管理系统
2021/07/01 Java/Android
php访问对象中的成员的实例方法
2021/11/17 PHP
前端canvas中物体边框和控制点的实现示例
2022/08/05 Javascript
LyScript实现绕过反调试保护的示例详解
2022/08/14 Python