Python编写电话薄实现增删改查功能
作者:net小伙 发布时间:2021-07-14 21:28:18
标签:Python,电话薄,增删改查
初学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提供了强大的url库,想做到这个并不难。这里以登录学校教务
- 本文实例为大家分享了Django实现上传图片的具体代码,供大家参考,具体内容如下1.设置存放上传的图片的文件夹settings.pyMEDI
- python合并文本文件示例代码。python实现两个文本合并employee文件中记录了工号和姓名cat employee.txt:100
- 视频对象提取与其说是视频对象提取,不如说是视频颜色提取,因为其本质还是使用了OpenCV的HSV颜色物体检测。下面话不多说了,来一起看看详细
- RPC(Remote Procedure Call Protocol)远程过程调用协议。 一个通俗的描述是:客户端在不知道调用细节的情况下,
- 在VsCode中ctrl+s后会在当前目录下自动生成dist目录解决办法:关闭compile-hero插件在设置中搜索compile-her
- 1、定义全局变'''全局变量: X 和 O 表示两方的棋子; EMPTY 表示棋位为空; TIE 表示平局; NUM
- 方法一、input标签上传如果是input标签,可以直接输入路径,那么可以直接调用send_keys输入路径,这里不做过多赘述,前文有相关操
- 我就废话不多说了,大家还是直接看代码吧!a1 = raw_input("please input a number")a
- 1. 复制表结构及其数据:create table table_name_new as select * from table_name_o
- 不好意思,标题比较啰嗦,因为这次的流水账确实属于一个比较细节的小东西,下面详细讲:1需求最近在使用electron-vue 开发一个跨平台的
- 前言前面一直使用命令行运行pytest用例,本篇来学下使用pytest.main()来运行测试用例pytest.main()args 传一个
- 1 解决方案【方案一】载入模型结构放在全局,即tensorflow会话外层。'''载入模型结构:最关键的一步'
- 这篇文章主要介绍了python匿名函数lambda原理及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 报错现象File "<string>", line 1SyntaxError: unexpected EOF
- json数据:[{"authenticate":-99,"last_ip":"156.2.
- python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。Python提供
- 之前我们已经安装了lnmp的环境,现在让我们来安装phpmyadmin。跟前一样,yum默认的库里是没有phpmyadmin的,我们需要从e
- 一个动态数组 a,如果你已经使用redim 语句给它设定了大小,那么在此之后使用 ubound(a) 就可以得到它的上边界。如果你没有使用
- 通常情况下:from threading import Threadglobal_num = 0def func1(): global gl