一个简单的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 相关文章推荐
Python3使用requests包抓取并保存网页源码的方法
Mar 15 Python
深入理解Python变量与常量
Jun 02 Python
详解Python开发中如何使用Hook技巧
Nov 01 Python
Python面向对象程序设计类的封装与继承用法示例
Apr 12 Python
Django 返回json数据的实现示例
Mar 05 Python
python爬虫把url链接编码成gbk2312格式过程解析
Jun 08 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
Jul 15 Python
Django如何在不停机的情况下创建索引
Aug 02 Python
Python 如何查找特定类型文件
Aug 17 Python
Python如何将模块打包并发布
Aug 30 Python
简单了解Python字典copy与赋值的区别
Sep 16 Python
Pyside2中嵌入Matplotlib的绘图的实现
Feb 22 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中4个加速、缓存扩展的区别和选用建议
2014/03/12 PHP
PHP实现将科学计数法转换为原始数字字符串的方法
2014/12/16 PHP
PHP请求远程地址设置超时时间的解决方法
2016/10/29 PHP
PHP利用正则表达式将相对路径转成绝对路径的方法示例
2017/02/28 PHP
PHP的cookie与session原理及用法详解
2019/09/27 PHP
PHP常用字符串输出方法分析(echo,print,printf及sprintf)
2021/03/09 PHP
Javascript在IE和FireFox中的不同表现简析
2012/12/03 Javascript
jquery入门必备的基本认识及实例(整理)
2013/06/24 Javascript
一个封装js代码-----展开收起效果示例
2013/07/03 Javascript
js实现特定位取反原理及示例
2014/06/30 Javascript
jQuery中detach()方法用法实例
2014/12/25 Javascript
javascript+canvas制作九宫格小程序
2014/12/28 Javascript
JS版元素周期表实现方法
2015/08/05 Javascript
JavaScript中Number对象的toFixed() 方法详解
2016/09/02 Javascript
Spring Boot+AngularJS+BootStrap实现进度条示例代码
2017/03/02 Javascript
详解使用nvm安装node.js
2017/07/18 Javascript
Vue.js 事件修饰符的使用教程
2018/11/01 Javascript
CryptoJS中AES实现前后端通用加解密技术
2018/12/18 Javascript
微信小程序自定义组件传值 页面和组件相互传数据操作示例
2019/05/05 Javascript
微信小程序如何获取群聊的openGid以及名称详解
2019/07/17 Javascript
python删除过期文件的方法
2015/05/29 Python
Python编程实战之Oracle数据库操作示例
2017/06/21 Python
Pycharm无法显示动态图片的解决方法
2018/10/28 Python
python实现K近邻回归,采用等权重和不等权重的方法
2019/01/23 Python
Python读取stdin方法实例
2019/05/24 Python
python flask框架实现重定向功能示例
2019/07/02 Python
记一次高分屏下canvas模糊问题
2020/02/17 HTML / CSS
护士的岗位职责
2013/12/04 职场文书
思想纪律作风整顿剖析材料
2014/10/11 职场文书
商业计划书范文
2019/04/24 职场文书
创业计划书之旅游网站
2019/09/06 职场文书
优秀范文:《但愿人长久》教学反思3篇
2019/10/24 职场文书
Pytest之测试命名规则的使用
2021/04/16 Python
python turtle绘图命令及案例
2021/11/23 Python
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
Mysql中@和@@符号的详细使用指南
2022/06/05 MySQL