【python】——小程序之电话薄

本文介绍了一个简单的Python程序,用于管理学生信息,包括增加、删除、修改和查询学生的姓名及电话号码。利用字典存储数据,并通过pickle进行序列化保存。

  初学python,写一个小程序练习一下。主要功能就是增删改查的一些功能。主要用到的技术:字典的使用,pickle的使用,io文件操作。代码如下:

 1 import pickle
 2 
 3 #studentinfo = {'netboy': '15011038018',\
 4 #                               'godboy': '15011235698'}
 5 studentinfo = {}
 6 
 7 FUNC_NUM = 5
 8 
 9 def write_file(value):
10         file = open('student_info.txt', 'wb')
11         file.truncate()
12         pickle.dump(value, file, True)
13         file.close
14 
15 def read_file():
16         global studentinfo
17         file = open('student_info.txt', 'rb')
18         studentinfo = pickle.load(file)
19         file.close()
20 
21 def search_student():
22         global studentinfo
23         name = input('please input student\'s name:')
24         if name in studentinfo:
25                 print('name:%s phone:%s' % (name, studentinfo[name]))
26         else:
27                 print('has no this body')
28 
29 def delete_student():
30         global studentinfo
31         name = input('please input student\'s name:')
32         if name in studentinfo:
33                 studentinfo.pop(name)
34                 write_file(studentinfo)
35         else:
36                 print('has no this body')
37 
38 def add_student():
39         global studentinfo
40         name = input('please input student\'s name:')
41         phone = input('please input phone:')
42         studentinfo[name] = phone
43         write_file(studentinfo)
44 
45 def modifiy_student():
46         global studentinfo
47         name = input('please input student\'s name:')
48         if name in studentinfo:
49                 phone = input('please input student\'s phone:')
50                 studentinfo[name] = phone
51         else:
52                 print('has no this name')
53 
54 def show_all():
55         global studentinfo
56         for key, value in studentinfo.items():
57                 print('name:' + key + 'phone:' + value)
58 
59 func = {1 : search_student, \
60         2 : delete_student, \
61         3 : add_student, \
62         4 : modifiy_student, \
63         5 : show_all}
64 
65 def menu():
66         print('-----------------------------------------------');
67         print('1 search student:')
68         print('2 delete student:')
69         print('3 add student:')
70         print('4 modifiy student:')
71         print('5 show all student')
72         print('6 exit')
73         print('-----------------------------------------------');
74 
75 def init_data():
76         global studentinfo
77         file = open('student_info.txt', 'rb')
78         studentinfo = pickle.load(file)
79         #print(studentinfo)
80         file.close()
81 
82 init_data()
83 while True:
84         menu()
85         index = int(input())
86         if index == FUNC_NUM + 1:
87                 exit()
88         elif index < 1 or index > FUNC_NUM + 1:
89                 print('num is between 1-%d' %  (FUNC_NUM + 1))
90                 continue
91         #print(index)
92         func[index]()

如有错误,或者更好的想法,请指教。

微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 微信小程序系统教程共有“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]——核心技术”、“微信小程序系统教程[阶段]客服消息+微信支付+九宝电商系统”。 “微信小程序系统教程[阶段]全套课程”包含: 1.微信小程序系统教程[阶段]_客服消息 2.微信小程序系统教程[阶段]_微信支付 3.微信小程序系统教程[阶段]_九宝电商系统 学习“微信小程序系统教程[阶段]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]”,后在报名“微信小程序系统教程[阶段]”。 阶段讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,没有使用三方技术。这样降低同学们学习成本。 微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。 九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。 “微信小程序系统教程[中级阶段]——核心技术”重点讲解微信小程序事件、组件、API 微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。 购课请咨询qq2326321088
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值