编写学生管理系统,实现增删改查
# 输入数字1,添加学生信息(编号, 名字,年纪,性别)
# 第一个学生id为101 后续学生自动加1
# 输入数字2,查看所有学生信息
# 输入数字3,查看指定学生信息
# 输入学生id,显示对应学生信息
# 输入数字4,修改学生信息
# 输入学生id,输入学生新名字,新年纪,新性别
# 输入数字5,删除指定学生
# 输入学生id,删除指定学生
# 输入数字0,退出系统
# 列表存储所有学生
students = [
{"id": 101, "name": "马云", "age": 20, "sex": "男"}
]
menu = """输入数字1,添加学生信息(编号, 名字,年纪,性别)
输入数字2,查看所有学生信息
输入数字3,查看指定学生信息
输入数字4,修改学生信息
输入数字5,删除指定学生
输入数字0,退出系统"""
while True:
print(menu)
option = input("输入数字,选择对应的选项")
if option not in [str(i) for i in range(6)]:
print(f"输入不合法,请输入指定的数字")
else:
if option == "0":
break
elif option == "1":
while True:
sid = 101 if not students else students[-1]["id"] + 1
while True:
name = input("请输入姓名")
if 2 <= len(name) <= 4:
break
else:
print(f"名字不合法,长度2-4个字符")
while True:
age = int(input("输入年纪"))
if 15 <= age <= 25:
break
else:
print(f"年纪不合法, 范围15-25")
while True:
sex = input("请输入性别")
if sex in ["男", "女"]:
break
else:
print(f"性别不合法,只能输入男、女")
s = {
"id": sid,
"name": name,
"age": age,
"sex": sex
}
students.append(s)
print(f"添加学生成功, 现有学生 {len(students)} 人")
while True:
sub_option = input("输入数字,选择对应的选项。Y(继续添加) N(返回)")
if sub_option not in ["Y", "y", "N", "n"]:
print(f"输入不合法,只能输入Y、N")
else:
break
if sub_option == "Y" or sub_option == "y":
pass
elif sub_option == "N" or sub_option == "n":
break
elif option == "2":
if not students:
print(f"还没有添加学生信息")
else:
for s in students:
print(f"id:{s.get('id')}\tname:{s.get('name')}\tage:{s.get('age')}\tsex:{s.get('sex')}")
elif option == "3":
while True:
sid = int(input("输入要查看学生的id"))
for s in students:
if sid == s.get("id"):
print(f"id:{s.get('id')}\tname:{s.get('name')}\tage:{s.get('age')}\tsex:{s.get('sex')}")
break
else:
print(f"没有找到id为{sid}的学生")
while True:
sub_option = input("输入数字,选择对应的选项。Y(继续查看) N(返回)")
if sub_option not in ["Y", "y", "N", "n"]:
print(f"输入不合法,只能输入Y、N")
else:
break
if sub_option == "Y" or sub_option == "y":
pass
elif sub_option == "N" or sub_option == "n":
break
elif option == "4":
while True:
sid = int(input("输入要修改学生的id"))
for s in students:
if sid == s.get("id"):
while True:
name = input("请输入姓名")
if 2 <= len(name) <= 4:
break
else:
print(f"名字不合法,长度2-4个字符")
while True:
age = int(input("输入年纪"))
if 15 <= age <= 25:
break
else:
print(f"年纪不合法, 范围15-25")
while True:
sex = input("请输入性别")
if sex in ["男", "女"]:
break
else:
print(f"性别不合法,只能输入男、女")
s['name'] = name
s['age'] = age
s['sex'] = sex
print(f"修改成功")
break
else:
print(f"没有找到id为{sid}的学生")
while True:
sub_option = input("输入数字,选择对应的选项。Y(继续修改) N(返回)")
if sub_option not in ["Y", "y", "N", "n"]:
print(f"输入不合法,只能输入Y、N")
else:
break
if sub_option == "Y" or sub_option == "y":
pass
elif sub_option == "N" or sub_option == "n":
break
elif option == "5":
while True:
sid = int(input("输入要删除学生的id"))
for s in students:
if sid == s.get("id"):
students.remove(s)
print(f"删除成功 现有学生 {len(students)} 人")
break
else:
print(f"没有找到id为{sid}的学生")
while True:
sub_option = input("输入数字,选择对应的选项。Y(继续删除) N(返回)")
if sub_option not in ["Y", "y", "N", "n"]:
print(f"输入不合法,只能输入Y、N")
else:
break
if sub_option == "Y" or sub_option == "y":
pass
elif sub_option == "N" or sub_option == "n":
break
标签:数字,管理系统,改查,信息,学生,增删,id,输入,option
From: https://blog.csdn.net/zhangzhaoyuxunlei/article/details/140444895