编写学生管理系统,实现增删改查

编写学生管理系统,实现增删改查
# 输入数字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

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农NoError

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值