版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30072697/article/details/81229221
一个小作业
其实没什么难度
只是分成函数调用就行了
我这还不算模块化 我觉得是那种每个人做一个功能 便于多人协作
# 2.编写“学生管理系统”,要求如下:
# 必须使用自定义函数,完成对程序的模块化
# 学生信息至少包含:姓名、年龄、学号,除此以外可以适当添加
# 必须完成的功能:添加、删除、修改、查询(单个查询/显示所有)、退出
# 增加数据
def fun_add(olddb, age, name, number, **kwargs):
# print(len(kwargs))
if number not in olddb:
kwargs['姓名'] = name
kwargs['年龄'] = age
olddb[number] = kwargs
else:
print('[警告]>>>学号已存在,不能添加!')
# print(olddb)
return olddb
# 删除数据
def fun_del(info_all, number):
if number in info_all:
del info_all[number]
if number not in info_all:
print('[信息]>>>操作成功,已删除学号[%s] ' % number)
return info_all
else:
rint('[警告]>>>操作失败,请重新操作!')
else:
print('[警告]>>>学号不存在,请重新操作!')
# 更改数据
def fun_revise(info_all, number, updata_dict):
if number in info_all:
info_all[number].update(updata_dict)
print('[信息]>>>操作成功,学号[%s]信息已修改' % number)
return info_all
else:
print('[警告]>>>学号不存在,请重新操作!')
# 查找单个信息
def fun_searchone(info_all, number):
if number in info_all:
print('[信息]>>>学号:', number, info_all[number])
else:
print('[警告]>>>学号不存在,请重新操作!')
# 查看所有信息
def fun_searchall(info_all):
for (i, j) in info_all.items():
print('[信息]>>>学号:', i, j)
def fun_main():
notice = ''' -------------------------------------------
| 功能菜单 |
-------------------------------------------
| 1.添加 | 2.删除 | 3.修改 | 4.查询 | 0.退出 |
-------------------------------------------
'''
info_all = {'0': {'姓名': '李四', '年龄': 20, '性别': '男'}, '1': {'姓名': '张三', '年龄': 15, '性别': '男'},
'2': {'姓名': '李倩', '年龄': 18, '性别': '女', '政治面貌': '团员', '班级职务': '班长'}}
print(' -------------------------------------------')
print('| 欢迎使用XX学生管理系统 |')
while True:
key_input = input(notice + '[提示]>>>请输入你的操作:') # 获取输入信息
if key_input == '0':
if input('[提示]>>>是否退出?(y/n)') == 'y': # 提示是否退出 防止误触
print('[信息]>>>欢迎下次使用 O(∩_∩)O ')
break
elif key_input == '1':
name = input('[提示]>>>请输入要添加的姓名')
number = input('[提示]>>>请输入要添加的学号')
age = input('[提示]>>>请输入要添加的年龄')
other_dict = {}
while True:
other_name = input('[提示]>>>请输入要添加的信息名,Q退出输入')
if other_name == 'Q' or other_name == 'q':
break
elif other_name != '':
other_info = input('[提示]>>>请输入要添加的信息内容')
if other_info != '':
other_dict[other_name] = other_info
else:
print('[警告]>>>输入字段不能为空!')
else:
print('[警告]>>>输入字段不能为空!')
info_all = fun_add(info_all, age=int(age), name=name, number=number, **other_dict)
elif key_input == '2':
number = input('[提示]>>>请输入要删除的学号:')
if number == '':
print('[警告]>>>输入学号不能为空!')
else:
select = input('[提示]>>>删除操作不可逆,不可恢复,是否确认删除?y/n:')
if select == 'y':
info_all = fun_del(info_all, number)
elif key_input == '3':
updata_dict = {}
number = input('[提示]>>>请输入要修改信息的学生的学号:')
while True:
updata_name = input('[提示]>>>请输入要修改信息的字段名[学号不可修改][Q退出]:')
if updata_name == 'Q' or updata_name == 'q':
break
else:
updata_info = input('[提示]>>>请输入要修改后的字段信息:')
if updata_info != '':
updata_dict[updata_name] = updata_info
else:
print('[警告]>>>输入的字段信息不能为空,请重新输入!')
print(updata_dict)
info_all = fun_revise(info_all, number, updata_dict)
elif key_input == '4':
choice = input('[提示]>>>请选择查询类型 [所有数据 1][单条数据 2]:')
if choice == '1':
fun_searchall(info_all)
elif choice == '2':
number = input('[提示]>>>请输入要查询的学号:')
if number != '':
fun_searchone(info_all, number)
else:
print('[警告]>>>查询的学号不能为空,请重新输入!')
else:
print('[警告]>>>你输入的信息有误,请重新输入!')
else:
print('[警告]>>>你输入的信息有误,请重新输入!')
fun_main()
运行结果
-------------------------------------------
| 欢迎使用XX学生管理系统 |
-------------------------------------------
| 功能菜单 |
-------------------------------------------
| 1.添加 | 2.删除 | 3.修改 | 4.查询 | 0.退出 |
-------------------------------------------
[提示]>>>请输入你的操作:4
[提示]>>>请选择查询类型 [所有数据 1][单条数据 2]:1
[信息]>>>学号: 0 {'姓名': '李四', '年龄': 20, '性别': '男'}
[信息]>>>学号: 1 {'姓名': '张三', '年龄': 15, '性别': '男'}
[信息]>>>学号: 2 {'姓名': '李倩', '年龄': 18, '性别': '女', '政治面貌': '团员', '班级职务': '班长'}
-------------------------------------------
| 功能菜单 |
-------------------------------------------
| 1.添加 | 2.删除 | 3.修改 | 4.查询 | 0.退出 |
-------------------------------------------
[提示]>>>请输入你的操作:
Process finished with exit code -1