#!/usr/bin/env python
#coding=utf-8
#电话本管理,可以进行添加,删除,修改,查询用户信息
import cPickle as p
class Telephone:
def __init__(self):
'''构造方法 '''
def addPeople(self,name,email,telephone):
"""添加用户"""
teleDict = self.getDictData()
if teleDict:
infoList = [name,email,telephone]
teleDict[name] = infoList
self.writefile(teleDict)
else:
teleDict = {}
infoList = [name,email,telephone]
teleDict[name] = infoList
self.writefile(teleDict)
def delPeople(self,name):
"""删除用户"""
teleDict = self.getDictData()
if name in teleDict.keys():
del teleDict[name]
self.writefile(teleDict)
else:
print name,'is not in dict'
def editPeople(self,name,emial,telephone):
""" 修改信息"""
teleDict = self.getDictData()
if name in teleDict.keys():
infoList = [name,email,telephone]
teleDict[name] = infoList
self.writefile(teleDict)
print name+'edit success'
else:
print name,'is not in dict'
def getPeople(self,name):
"""获取用户信息"""
teleDict = self.getDictData()
if teleDict:
if name in teleDict.keys():
people = teleDict[name]
print people
else:
print name,'is not in dict'
else:
print 'people is empty'
def writefile(self, dictData):
""" 写入文件"""
f = file('dict.data','w')
p.dump(dictData,f)
f.close()
def getDictData(self):
""" 获取文件内容"""
fileName = 'dict.data'
try:
f = file(fileName)
teleDict = p.load(f)
return teleDict
except:
print 'open file error'
# 提示 信息
def notice():
print "please enter 1-get people 2-add people 3-edit pelole 4-del people 5-get all people 0-break"
if __name__ == "__main__":
while(True):
notice()
userInput = int(raw_input())
people = Telephone()
if userInput == 1:
name = raw_input("please enter user name:")
people.getPeople(name)
elif userInput == 2:
name = raw_input("enter name:")
email = raw_input("enter emai:")
telephone = raw_input("enter telephone:")
people.addPeople(name,email,telephone)
elif userInput == 3:
name = raw_input("enter name:")
email = raw_input("enter emai:")
telephone = raw_input("enter telephone:")
people.editPeople(name,email,telephone)
elif userInput == 4:
name = raw_input("enter del people name:")
people.delPeople(name)
elif userInput == 5:
allpeople = people.getDictData()
if allpeople:
for key in allpeople:
print key,allpeople[key]
else:
print 'there is no people'
elif userInput == 0:
break
else:
print 'you select number is wrong'
raw_input('press enter') 0
0
相关文章
Python 中函数返回值的正确获取方式
python能做什么有趣的东西
RNN训练循环中每轮损失不变或异常上升的排查与修复
RNN训练循环中每轮损失值不变或异常上升的排查与修复
Faust 中 hopping window 表的正确使用方法与计数实现
python速学教程(入门到精通)
下载
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
Java 项目构建与依赖管理(Maven / Gradle)
本专题系统讲解 Java 项目构建与依赖管理的完整体系,重点覆盖 Maven 与 Gradle 的核心概念、项目生命周期、依赖冲突解决、多模块项目管理、构建加速与版本发布规范。通过真实项目结构示例,帮助学习者掌握 从零搭建、维护到发布 Java 工程的标准化流程,提升在实际团队开发中的工程能力与协作效率。
11
2026.01.12
热门下载
相关下载
精品课程
相关推荐
/
热门推荐
/
最新课程








