Python编写电话薄实现增删改查功能
作者:net小伙 时间:2021-07-14 21:28:18
初学python,写一个小程序练习一下。主要功能就是增删改查的一些功能。主要用到的技术:字典的使用,pickle的使用,io文件操作。代码如下:
import pickle
#studentinfo = {'netboy': '15011038018',\
# 'godboy': '15011235698'}
studentinfo = {}
FUNC_NUM = 5
def write_file(value):
file = open('student_info.txt', 'wb')
file.truncate()
pickle.dump(value, file, True)
file.close
def read_file():
global studentinfo
file = open('student_info.txt', 'rb')
studentinfo = pickle.load(file)
file.close()
def search_student():
global studentinfo
name = input('please input student\'s name:')
if name in studentinfo:
print('name:%s phone:%s' % (name, studentinfo[name]))
else:
print('has no this body')
def delete_student():
global studentinfo
name = input('please input student\'s name:')
if name in studentinfo:
studentinfo.pop(name)
write_file(studentinfo)
else:
print('has no this body')
def add_student():
global studentinfo
name = input('please input student\'s name:')
phone = input('please input phone:')
studentinfo[name] = phone
write_file(studentinfo)
def modifiy_student():
global studentinfo
name = input('please input student\'s name:')
if name in studentinfo:
phone = input('please input student\'s phone:')
studentinfo[name] = phone
else:
print('has no this name')
def show_all():
global studentinfo
for key, value in studentinfo.items():
print('name:' + key + 'phone:' + value)
func = {1 : search_student, \
2 : delete_student, \
3 : add_student, \
4 : modifiy_student, \
5 : show_all}
def menu():
print('-----------------------------------------------');
print('1 search student:')
print('2 delete student:')
print('3 add student:')
print('4 modifiy student:')
print('5 show all student')
print('6 exit')
print('-----------------------------------------------');
def init_data():
global studentinfo
file = open('student_info.txt', 'rb')
studentinfo = pickle.load(file)
#print(studentinfo)
file.close()
init_data()
while True:
menu()
index = int(input())
if index == FUNC_NUM + 1:
exit()
elif index < 1 or index > FUNC_NUM + 1:
print('num is between 1-%d' % (FUNC_NUM + 1))
continue
#print(index)
func[index]()
标签:Python,电话薄,增删改查
0
投稿
猜你喜欢
Python语言生成水仙花数代码示例
2022-11-16 18:17:48
python实现弹跳小球
2022-05-30 08:55:08
Python如何读取、写入CSV数据
2022-02-17 14:03:31
详细讲解HDFS的高可用机制
2023-11-13 03:37:47
理解HTTP消息头
2008-12-10 14:06:00
数据库查询优化(主从表的设计)
2024-01-18 16:25:52
Python Web程序搭建简单的Web服务器
2022-08-07 12:33:04
OpenCV 图像对比度的实践
2023-07-29 09:09:27
Python闭包的两个注意事项(推荐)
2023-11-29 14:01:09
Python中对数据库的操作详解
2024-01-19 18:26:54
python 多个参数不为空校验方法
2022-12-15 10:48:56
pyqt5简介及安装方法介绍
2022-05-21 15:18:53
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
2021-03-23 05:09:50
将图片读入到Dom中,并将其存为xml文件
2008-09-04 11:24:00
Oracle在PL/SQL中嵌入SQL语句
2024-01-19 03:06:03
解析python调用函数加括号和不加括号的区别
2023-11-30 17:29:50
python 基于 tkinter 做个学生版的计算器
2022-07-30 18:43:54
python中bottle使用实例代码
2023-07-03 14:21:15
用python3教你任意Html主内容提取功能
2022-09-08 02:27:01
Python 从相对路径下import的方法
2023-06-15 03:16:10