python学生名片管理vv2.0是在1.0的基础上增加部分功能,实现将数据存入文件保存,以便于程序停止后还能再次取到数据。具体实现请看如下部分:
card_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import sys from one.card_func2 import * def head(): print ( '*' * 30 ) print ( '学生信息管理系统v2.0' ) print ( '1.添加学生\t2.显示所有\t3.查询学生\t0.退出系统' ) print ( '*' * 30 ) def main(): while true: head() num = input ( '输入操作:' ) if num = = '1' : print ( '选择功能添加学生' ) add_card() elif num = = '2' : print ( '选择功能添显示所有' ) show_all() elif num = = '3' : print ( '选择功能查询学生' ) search_card() elif num = = '0' : sys.exit() else : print ( '输入有误,命令无效' ) if __name__ = = '__main__' : main() |
card_func2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import json import time stutent_list = [] def add_card(): name = input ( '请输入姓名:' ) age = input ( '请输入年龄:' ) qq = input ( '请输入qq:' ) mail = input ( '请输入邮箱:' ) dict = {} dict [ 'name' ] = name dict [ 'age' ] = age dict [ 'qq' ] = qq dict [ 'mail' ] = mail stutent_list.append( dict ) memory(stutent_list) def memory(stutent_list): # 将数据存入文件 with open ( 'student.json' , 'w' ) as f: for i in stutent_list: json_i = json.dumps(i) f.write(json_i + '\n' ) f.close() def repick(): # 从文件中去除数据 risk_result = [] with open ( 'student.json' , 'r' ) as f: risk_new_list = f.read().split( '\n' )[: - 1 ] for x in risk_new_list: json_x = json.loads(x) risk_result.append(json_x) f.close() return risk_result def show_all(): global stutent_list stutent_list = repick() table_head() for dict in stutent_list: print ( '-' * 32 ) print ( '| %s\t|\t%s\t|\t%s\t|\t%s\t|' % ( dict [ 'name' ], dict [ 'age' ], dict [ 'qq' ], dict [ 'mail' ])) def search_card(): global stutent_list name = input ( '请输入查找学生的姓名:' ) for dict in stutent_list: if dict [ 'name' ] = = name: table_head() print ( '-' * 32 ) print ( '| %s\t|\t%s\t|\t%s\t|\t%s\t|' % ( dict [ 'name' ], dict [ 'age' ], dict [ 'qq' ], dict [ 'mail' ])) print ( '您可以执行操作:1.删除\t2.修改\t3.返回上一级' ) deal_card( dict ) break else : print ( '查无此人!!!' ) def table_head(): print ( '_' * 32 ) print ( '| 姓名\t|\t年龄\t|\tqq\t|\t邮箱\t|' ) def deal_card( dict ): order = input ( '请输入命令:' ) while true: if order = = '1' : delete_card( dict ) break elif order = = '2' : modify_card( dict ) break elif order = = '3' : break else : print ( '输入有误,命令无效' ) def delete_card( dict ): print ( '执行命令,删除数据' ) stutent_list.remove( dict ) print ( '...正在删除,请等待...' ) turntime() print ( '删除成功,返回主界面' ) def modify_card( dict ): print ( '执行命令,修改数据' ) dict [ 'name' ] = input ( '请输入姓名:' ) dict [ 'age' ] = input ( '请输入年龄:' ) dict [ 'qq' ] = input ( '请输入qq:' ) dict [ 'mail' ] = input ( '请输入邮箱:' ) print ( '...正在修改,请等待...' ) turntime() print ( '修改成功,返回主界面' ) def turntime(): for i in list ( range ( 5 , 0 , - 1 )): print (i, 's' ) time.sleep( 1 ) |
以下部分为控制台效果截图:
2.查询
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/CSDNgaoqingrui/article/details/80529003