def add_stu():
"""
添加学员
:return: None
"""
while True:
name = input(\'* 请输入学员姓名:\')
age = input(\'* 请输入学员年龄:\')
phone = input(\'* 请输入学员电话:\')
# 将三条信息组装为一个字典
stu = {\'name\': name, \'age\': age, \'phone\': phone}
# 将小字典放入大列表中
students.append(stu)
is_next = input(\'回车继续添加,输入q结束:\')
if is_next == \'q\':
break
def query_all_stu():
"""
查询所有学员信息
:return: None
"""
# 判断是否有学员信息
if len(students) == 0:
print(\'* 没有学员信息,请稍后重试~ \')
return
# 遍历所有学员信息
print(\'* 查询结果如下:\')
# enumerate() 将列表进行枚举,会得到索引和对应位置的元素组成的元组
# idx, stu 将两个变量和元组中的数据一一对应
for idx, stu in enumerate(students):
# idx 是小字典的索引 stu是小字典
print(\'* 索引:%s 姓名:%s 年龄:%s 电话:%s \' % (idx + 1, stu[\'name\'], stu[\'age\'], stu[\'phone\']))
def query_of_keyword():
"""
根据关键词搜索学员信息
:return: None
"""
# 判断是否有学员信息
if len(students) == 0:
print(\'* 没有学员信息,请稍后重试~ \')
return
# 输入一个关键词
kw = input(\'* 请输入要搜索的关键词:\')
# 声明变量用来记录找到的个数
count = 0
for idx, stu in enumerate(students):
# 判断关键词是否在学员姓名中
if kw in stu[\'name\']:
count += 1
print(\'* 索引:%s 姓名:%s 年龄:%s 电话:%s\' % (idx + 1, stu[\'name\'], stu[\'age\'], stu[\'phone\']))
# 判断是否找到结果
if count == 0:
print(\'* 没有找到结果~ \')
else:
print(\'* 共找到%s个结果! \' % count)
def query_stu():
"""
查询学员
:return: None
"""
print(\'* a.关键字查找\')
print(\'* b.查找所有学员\')
select = input(\'* 请选择查询方式:\')
while select != \'a\' and select != \'b\':
select = input(\'* 选项有误,请重选:\')
if select == \'a\':
query_of_keyword()
else:
query_all_stu()
def modify_stu():
"""
修改学员信息
:return:None
"""
if len(students) == 0:
print(\'* 没有学员信息,无法执行修改操作!\')
return
# 1.展示所有学员信息
query_all_stu()
# 2.选择要修改的学员索引,判断索引是否在范围
idx = int(input(\'* 请选择要修改的学员索引:\'))
while idx < 1 or idx > len(students):
idx = int(input(\'* 索引有误,请重选:\'))
# 3.根据索引取出小字典
stu = students[idx - 1]
stu[\'name\'] = input(\'* 请输入修改后的姓名(%s):\' % stu[\'name\'])
stu[\'age\'] = input(\'* 请输入修改后的年龄(%s):\' % stu[\'age\'])
stu[\'phone\'] = input(\'* 请输入修改后的电话(%s):\' % stu[\'phone\'])
print(\'* 修改完成!\')
def delete_stu():
"""
删除学员
:return: None
"""
if len(students) == 0:
print(\'* 没有学员信息,无法执行删除操作~ \')
return
print(\'* a.选择索引删除\')
print(\'* b.删除所有学员\')
select = input(\'* 请选择删除方式:\')
while select != \'a\' and select != \'b\':
select = input(\'* 选项有误,请重选:\')
if select == \'a\':
# 1.展示所有学员信息
query_all_stu()
# 2.选择要删除的索引
idx = int(input(\'* 输入要删除的学员索引:\'))
while idx < 1 or idx > len(students):
idx = int(input(\'* 索引有误,请重选:\'))
# 3.取出选择的数据,确认是否删除
stu = students[idx - 1]
is_del = input(\'* 确认要删除(%s)?y/n:\' % stu[\'name\'])
if is_del == \'y\':
del students[idx - 1]
print(\'* 删除成功!\')
else:
# 删除列表中所有数据
# del students[:]
while len(students):
students.pop()
print(\'* 删除成功!\')
# 存储所有学员信息的大列表
students = []
# True(可以用数字1表示) False(可以用数字0表示) 布尔类型数据
while True:
print(\'*********智游学员管理V1.0*********\')
print(\'* 1.添加学员 *\')
print(\'* 2.修改学员 *\')
print(\'* 3.删除学员 *\')
print(\'* 4.查询学员 *\')
print(\'* 0.退出程序 *\')
select = int(input(\' 请选择您的操作:\'))
while select <0 or select > 4:
select = int(input(\' 选择有误,请重选:\'))
print(\'**********************************\')
if select == 1:
add_stu()
elif select == 2:
modify_stu()
elif select == 3:
delete_stu()
elif select == 4:
query_stu()
else:
print(\'* 感谢您的使用,下次再会! *\')
break