完成一个控制台学生管理系统,要求能实现查看所有学生的学号、姓名、年龄、电话,并且能根据学号和姓名进行查询,能修改学生的信息,能删除学生信息
all_students = [
{'no':'stu001','name':'stu1','age':18,'phone':'1811111111111'},
{'no':'stu002','name':'stu2','age':20,'phone':'1811111100000'},
{'no':'stu003','name':'stu3','age':22,'phone':'1811111100011'}]
wellcome = """
========================================
欢迎进入学生管理系统:
❤ 1. 添加学生
❤ 2. 查看学生
❤ 3. 修改学生信息
❤ 4. 删除学生
❤ 5. 退出
========================================
请输入(1-5):
"""
wellcome2 = """
1. 查看所有学生
2. 按姓名查找
3. 按学号查找
4. 返回
"""
while True:
menu =int(input(wellcome))
if menu == 1:
sub_menu = 1
while sub_menu != 2:
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
phone = input("请输入学生电话:")
no = 'stu%03d' % (len(all_students) + 1)
student = {
'no':no,
'name':name,
'age':int(age),
'phone':phone
}
all_students.append(student)
print("添加成功!")
sub_menu =int(input("1. 继续\n2. 返回\n请选择(1-2):"))
elif menu == 2:
sub_menu = int(input(wellcome2))
while sub_menu!=4:
if sub_menu == 1:
for student in all_students:
print("学号:%s\t姓名:%s\t年龄:%d\t电话:%s" % (student["no"],student["name"],student["age"],student["phone"]))
elif sub_menu == 2:
name = input("请输入要查找的姓名:")
for student in all_students:
if student["name"]== name:
print("学号:%s\t姓名:%s\t年龄:%d\t电话:%s" % (student["no"],student["name"],student["age"],student["phone"]))
elif sub_menu == 3:
no = input("请输入要查找的学号:")
for student in all_students:
if student["no"]== no:
print("学号:%s\t姓名:%s\t年龄:%d\t电话:%s" % (student["no"],student["name"],student["age"],student["phone"]))
break
else:
print("查无此人")
sub_menu = int(input(wellcome2))
elif menu == 3:
sub_menu = 1
while sub_menu != 2:
no = input("请输入要修改的学生学号:")
for student in all_students:
if student["no"]== no:
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
phone = input("请输入学生电话:")
student["name"] = name
student["age"] = int(age)
student["phone"] = phone
break
else:
print("查无此人")
print("修改成功")
sub_menu = int(input("1. 继续\n2. 返回\n请选择(1-2):"))
elif menu == 4:
sub_menu = 1
while sub_menu != 2:
no = input("请输入要删除的学生学号:")
for student in all_students[:]:
if student["no"]== no:
all_students.remove(student)
break
else:
print("查无此人")
print("删除成功")
sub_menu = int(input("1. 继续\n2. 返回\n请选择(1-2):"))
else:
print("Bye~")
break