# -*- coding: utf-8 -*-
__author__ = 'wj'
__date__ = '2018/7/7 16:08'
def add_stu():
"""
添加学员
:return: None
"""
# 循环添加
while True:
name = input('* 请输入要添加的姓名:')
age = input('* 请输入要添加的年龄:')
phone = input('* 请输入要添加的电话:')
# 将三条数据组成一个小列表
stu = [name, age, phone]
# 将小列表添加到大列表中
students.append(stu)
is_next = input('是否继续添加?y/n:')
if is_next != 'y':
# 结束循环
break
def query_all_stu():
"""
查询所有学员信息
:return: None
"""
if len(students) == 0:
print('* 没有学员信息,请稍后再查~ *')
# return 1.强制结束函数执行,return之后的代码不会再执行
return
# 遍历查找所有的学员信息
print('* 查找结果如下: *')
for x in range(0, len(students)):
# stu就是 [姓名, 年龄, 电话]小列表
stu = students[x]
print('* 索引:%s 姓名:%s 年龄:%s 电话:%s' % (x+1, stu[0], stu[1], stu[2]))
def query_of_keyword():
"""
根据姓名关键词查找学员
:return:None
"""
if len(students) == 0:
print('* 没有学员信息,请稍后再查~ *')
return
# 1.输入搜索的关键词
kw = input('* 请输入要搜索的关键词:')
print('* 查找结果如下: *')
# 统计查找到的结果条数
count = 0
# 2.遍历大列表
for x in range(0, len(students)):
# stu就是[姓名,年龄,电话] 小列表
stu = students[x]
# 判断stu小列表中的姓名
if kw in stu[0]:
# 让计数+1
count += 1
print('* 索引:%s 姓名:%s 年龄:%s 电话:%s' % (x+1, stu[0], stu[1], stu[2]))
# 3.如果count在循环结束之后依然是0,说明没有找到符合条件的结果
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_all_stu()
else:
# 根据关键词进行查询
query_of_keyword()
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]
new_name = input('* 请输入修改后的学员姓名(%s):' % stu[0])
new_age = input('* 请输入修改后的学员年龄(%s):' % stu[1])
new_phone = input('* 请输入修改后的学员电话(%s):' % stu[2])
# 修改信息
stu[0] = new_name
stu[1] = new_age
stu[2] = new_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[0])
if is_del == 'y':
# 4.删除学员
del students[idx - 1]
print('* 删除操作执行成功 !')
else:
is_del = input('* 确认删除所有学员?y/n:')
if is_del == 'y':
# 删除所有学员信息
# 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