一个简单的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实现实例
Jun 20 Python
Tornado协程在python2.7如何返回值(实现方法)
Jun 22 Python
详解 Python 读写XML文件的实例
Aug 02 Python
python实现比较文件内容异同
Jun 22 Python
django项目搭建与Session使用详解
Oct 10 Python
python 判断参数为Nonetype类型或空的实例
Oct 30 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
Aug 26 Python
Python编程快速上手——Excel表格创建乘法表案例分析
Feb 28 Python
屏蔽Django admin界面添加按钮的操作
Mar 11 Python
Python 用户输入和while循环的操作
May 23 Python
Pytorch中的学习率衰减及其用法详解
Jun 05 Python
Python游戏开发实例之graphics实现AI五子棋
Nov 01 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将数据库导出成excel的方法
2010/05/07 PHP
php截取后台登陆密码的代码
2012/05/05 PHP
基于php缓存的详解
2013/05/15 PHP
深入解析yii权限分级式访问控制的实现(非RBAC法)
2013/06/13 PHP
php全角字符转换为半角函数
2014/02/07 PHP
54个提高PHP程序运行效率的方法
2015/07/19 PHP
Joomla开启SEF的方法
2016/05/04 PHP
[原创]解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载)
2016/06/22 PHP
基于ThinkPHP5.0实现图片上传插件
2017/09/25 PHP
JavaScript实现表格排序方法
2013/06/14 Javascript
纯js分页代码(简洁实用)
2013/11/05 Javascript
javascript去掉前后空格的实例
2013/11/07 Javascript
页面元素绑定jquery toggle后元素隐藏的解决方法
2014/03/27 Javascript
jquery datepicker参数介绍和示例
2014/04/15 Javascript
jq实现左侧显示图片右侧文字滑动切换效果
2015/08/04 Javascript
JS hashMap实例详解
2016/05/26 Javascript
JavaScript浏览器对象之一Window对象详解
2016/06/03 Javascript
js/jquery控制页面动态加载数据 滑动滚动条自动加载事件的方法
2017/02/08 Javascript
JavaScript禁止微信浏览器下拉回弹效果
2017/05/16 Javascript
一步一步的了解webpack4的splitChunk插件(小结)
2018/09/17 Javascript
JS实现简单日历特效
2020/01/03 Javascript
原生JS实现萤火虫效果
2020/03/07 Javascript
vue浏览器返回监听的具体步骤
2021/02/03 Vue.js
在Python的Flask框架下使用sqlalchemy库的简单教程
2015/04/09 Python
python实现图片变亮或者变暗的方法
2015/06/01 Python
Python 实现向word(docx)中输出
2020/02/13 Python
python3 logging日志封装实例
2020/04/08 Python
windows10在visual studio2019下配置使用openCV4.3.0
2020/07/14 Python
CSS3 box-sizing属性
2009/04/17 HTML / CSS
总裁助理岗位职责
2014/02/17 职场文书
股权投资意向书
2014/04/01 职场文书
销售人才自我评价范文
2014/09/27 职场文书
2014年变电站工作总结
2014/12/19 职场文书
大学团日活动总结书
2015/05/11 职场文书
MySQL表的增删改查基础教程
2021/04/07 MySQL
Mysql中存储引擎的区别及比较
2021/06/04 MySQL