一个简单的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使用urllib2获取网络资源实例讲解
Dec 02 Python
Python version 2.7 required, which was not found in the registry
Aug 26 Python
Linux下使用python调用top命令获得CPU利用率
Mar 10 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
May 14 Python
详解Python之数据序列化(json、pickle、shelve)
Mar 30 Python
python web.py开发httpserver解决跨域问题实例解析
Feb 12 Python
python删除某个字符
Mar 19 Python
对pandas中to_dict的用法详解
Jun 05 Python
详解关于Django中ORM数据库迁移的配置
Oct 08 Python
python实现汽车管理系统
Nov 30 Python
python爬虫 execjs安装配置及使用
Jul 30 Python
使用scrapy实现增量式爬取方式
Jun 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新手上路(十二)
2006/10/09 PHP
php批量删除数据
2007/01/18 PHP
php数组函数序列之end() - 移动数组内部指针到最后一个元素,并返回该元素的值
2011/10/31 PHP
CodeIgniter CLI模式简介
2014/06/17 PHP
YII视图整合kindeditor扩展的方法
2016/07/13 PHP
PHP微商城开源代码实例
2019/03/27 PHP
基于Laravel 多个中间件的执行顺序详解
2019/10/21 PHP
JavaScript 版本自动生成文章摘要
2008/07/23 Javascript
Prototype PeriodicalExecuter对象 学习
2009/07/19 Javascript
不用构造函数(Constructor)new关键字也能实现JavaScript的面向对象
2013/01/11 Javascript
返回顶部按钮响应滚动且动态显示与隐藏
2014/10/14 Javascript
jquery获得当前html页面源码的方法
2015/07/14 Javascript
将页面table内容与样式另存成excel文件的方法
2015/08/05 Javascript
JavaScript中函数声明与函数表达式的区别详解
2016/08/18 Javascript
jstree创建无限分级树的方法【基于ajax动态创建子节点】
2016/10/25 Javascript
微信小程序 loading 详解及实例代码
2016/11/09 Javascript
Bootstrap table右键功能实现方法
2017/02/20 Javascript
纯JS单页面赛车游戏制作代码分享
2017/03/03 Javascript
JS中Map和ForEach的区别
2018/02/05 Javascript
Vue 页面切换效果之 BubbleTransition(推荐)
2018/04/08 Javascript
Vue filter 过滤当前时间 实现实时更新效果
2019/12/20 Javascript
js模拟实现烟花特效
2020/03/10 Javascript
解决Vue 移动端点击出现300毫秒延迟的问题
2020/07/21 Javascript
vuex分模块后,实现获取state的值
2020/07/26 Javascript
对于Python装饰器使用的一些建议
2015/06/03 Python
python使用arcpy.mapping模块批量出图
2017/03/06 Python
Python中查看变量的类型内存地址所占字节的大小
2019/06/26 Python
基于Django OneToOneField和ForeignKey的区别详解
2020/03/30 Python
美国在线鲜花速递:ProFlowers
2017/01/05 全球购物
欧洲最大的拼图游戏商店:JigsawPuzzle.co.uk
2018/07/04 全球购物
顺丰快递Java软件工程师面试题
2015/07/31 面试题
教师节活动主持词
2014/04/02 职场文书
国际贸易本科毕业生求职信
2014/09/26 职场文书
年会邀请函的格式及范文五篇
2019/11/02 职场文书
Mysql中存储引擎的区别及比较
2021/06/04 MySQL
详解Mysq MVCC多版本的并发控制
2022/04/29 MySQL