相当于一个大列表,一个列表中有规定有限数量的键值对
def info_print():
print('请选择功能')
print('1.添加学员')
print('2.删除学员')
print('3.修改学员')
print('4.查询学员')
print('5.显示所有学员')
print('6.退出系统')
print('-'*20)
info = []
def add_info():
"""添加学院函数"""
new_id = input('请输入学号')
new_name = input('请输入姓名')
new_tel = input('请输入手机号')
global info
for i in info:
if new_name == i['name']:
print('此用户已经存在')
return
info_dict = {}
# 列表追加字典
info_dict['id'] = new_id
info_dict['name'] = new_name
info_dict['tel'] = new_tel
info.append(info_dict)
print(info)
while True:
info_print()
user_num = int(input('请输入功能序号:'))
if user_num == 1:
# print('添加')
add_info()
elif user_num == 2:
print('删除')
elif user_num == 3:
print('修改')
elif user_num == 4:
print('查询')
elif user_num == 5:
print('显示所有学员')
elif user_num == 6:
print('退出系统')
else:
print('输入功能序号有误')
for else
当循环正常运行结束之后,else才运行
def info_print():
print('请选择功能')
print('1.添加学员')
print('2.删除学员')
print('3.修改学员')
print('4.查询学员')
print('5.显示所有学员')
print('6.退出系统')
print('-'*20)
info = []
def add_info():
"""添加学院函数"""
new_id = input('请输入学号')
new_name = input('请输入姓名')
new_tel = input('请输入手机号')
global info
for i in info:
if new_name == i['name']:
print('此用户已经存在')
return
info_dict = {}
# 列表追加字典
info_dict['id'] = new_id
info_dict['name'] = new_name
info_dict['tel'] = new_tel
info.append(info_dict)
print(info)
def del_info():
del_name = input('请输入要删除的学员的姓名')
global info
for i in info:
if del_name == i['name']:
info.remove(i)
break
else:
print('用户不存在')
print(info)
def modify_info():
input_name = input("请输入你要修改学员的姓名")
global info
for i in info:
if input_name == i['name']:
i['tel'] = input('请输入新的手机号')
break
else:
print("学员不存在")
print(info)
def print_info():
global info
print(info)
def search_info():
search_name = input('请你输入查询名字')
global info
for i in info:
if search_name == i['name']:
print(f"该学员的学号是{i['id']},姓名是{i['name']},手机号是{i['tel']}")
break
else:
print('此用户不存在')
while True:
info_print()
user_num = int(input('请输入功能序号:'))
if user_num == 1:
# print('添加')
add_info()
elif user_num == 2:
# print('删除')
del_info()
elif user_num == 3:
# print('修改')
modify_info()
elif user_num == 4:
# print('查询')
search_info()
elif user_num == 5:
# print('显示所有学员')
print_info()
elif user_num == 6:
# print('退出系统')
print('正在退出系统')
exit()
else:
print('输入功能序号有误')