python的艰难学习之路-师生管理系统

师生管理系统

和我之前上传的名片管理系统差不多,只不过里面添加了一些面向对象的部分。

目前为V1,还存在一些优化问题未解决:
1.没有加入文件的读取,每次运行都是从头开始
2.编号可以自动生成,但是还没有解决如果有删除的情况,编号自动整理功能
3.未添加师生绑定的功能

代码块

teacher_list = []
student_list = []
stu_num = 1
tea_num = 1


def show_head_table():
    """
    显示主功能菜单
    """
    print("*"*50)
    print("欢迎使用师生信息管理系统V1.0")
    print("\r")
    print("        操作功能列表")
    print("\r")
    print("1.显示功能")
    print("2.新增功能")
    print("3.查询功能")
    print("\r")
    print("0.退出系统")
    print("*"*50)


def print_head():
    """每次查询或者新增时,显示表头"""
    print("内部编号", "编号", "姓名", "性别", "电话", sep=" "*20)
    print("——"*27)


def show_all_info():
    """显示所有师生信息"""
    print("\r")
    print("学生信息:")
    print_head()
    for stu_dic in student_list:
        print(stu_dic["inside_num"], end=" "*23)
        print(stu_dic["number"], end=" "*19)
        print(stu_dic["name"], end=" "*(24-len(stu_dic["name"])))
        print(stu_dic["sex"], end=" "*22)
        print(stu_dic["tel"])

    print("\r")
    print("教师信息:")
    print_head()
    for tea_dic in teacher_list:
        print(tea_dic["inside_num"], end=" "*23)
        print(tea_dic["number"], end=" "*19)
        print(tea_dic["name"], end=" "*(24-len(tea_dic["name"])))
        print(tea_dic["sex"], end=" "*22)
        print(tea_dic["tel"])


def search():
    tmp_search_list = []
    search_result = 0
    while True:
        your_choice_2 = input("请输入您要查询的姓名:")
        for stu_dic in student_list:
            if stu_dic["name"] == your_choice_2:
                search_result += 1
                stu_dic["tmp_number"] = search_result
                stu_dic["tmp_type"] = "student"
                tmp_search_list.append(stu_dic)
        for tea_dic in teacher_list:
            if tea_dic["name"] == your_choice_2:
                search_result += 1
                tea_dic["tmp_number"] = search_result
                tea_dic["tmp_type"] = "teacher"
                tmp_search_list.append(tea_dic)
        if search_result == 0:
            if_will_search_code = input("您查询的结果不存在是,是否要继续查询·(直接回车继续查询,输入任何内容则退出查询)")
            if if_will_search_code == "":
                continue
            else:
                break

        else:
            print("以下是您查询出来的信息:")
            print("序号", "类别", "编号", "姓名", "性别", "电话", sep=" " * 15)
            print("——" * 25)
            for tmp_dic in tmp_search_list:
                print(tmp_dic["tmp_number"], end=" "*18)
                print(tmp_dic["tmp_type"], end=" "*12)
                print(tmp_dic["number"], end=" "*14)
                print(tmp_dic["name"], end=" "*(19-len(tmp_dic["name"])))
                print(tmp_dic["sex"], end=" "*17)
                print(tmp_dic["tel"])
            print("——"*25)
            if len(tmp_search_list) == 1:  # 针对如果只查询出来一条信息
                your_choice_3 = input("请选择您需要对查询出来的信息的操作:1-->修改||2-->删除||3-->只是看看:")
                if your_choice_3 == "1":
                    print("序号、类别、编号无法修改,系统自动生成")
                    if tmp_search_list[0]["tmp_type"] == "student":
                        for i in range(len(student_list)):
                            if student_list[i]["number"] == tmp_search_list[0]["number"]:

                                edit_name = input("请输入新的名字(直接回车不做修改):")
                                if edit_name == "":
                                    print("名字未做修改!!")
                                else:
                                    student_list[i]["name"] = edit_name
                                edit_sex = input("请选择新的性别:1-->男||2-->女(直接回车不做修改):")
                                if edit_sex == "1":
                                    student_list[i]["sex"] = "男"
                                elif edit_sex == "2":
                                    student_list[i]["sex"] = "女"
                                elif edit_sex == "":
                                    print("性别未做修改!!")
                                edit_tel = input("请输入新的电话:(直接回车不做修改):")
                                if edit_tel == "":
                                    print("电话为做修改!!")
                                else:
                                    student_list[i]["tel"] = edit_tel
                                print("修改完成!!!")
                    elif tmp_search_list[0]["tmp_type"] == "teacher":
                        for i in range(len(teacher_list)):
                            if teacher_list[i]["number"] == tmp_search_list[0]["number"]:

                                edit_name = input("请输入新的名字(直接回车不做修改):")
                                if edit_name == "":
                                    print("名字未做修改!!")
                                else:
                                    teacher_list[i]["name"] = edit_name
                                edit_sex = input("请选择新的性别:1-->男||2-->女(直接回车不做修改):")
                                if edit_sex == "1":
                                    teacher_list[i]["sex"] = "男"
                                elif edit_sex == "2":
                                    teacher_list[i]["sex"] = "女"
                                elif edit_sex == "":
                                    print("性别未做修改!!")
                                edit_tel = input("请输入新的电话:(直接回车不做修改):")
                                if edit_tel == "":
                                    print("电话为做修改!!")
                                else:
                                    teacher_list[i]["tel"] = edit_tel
                                print("修改完成!!!")
                elif your_choice_3 == "2":
                    your_choice_4 = input("请确认是否要删除该条记录,删除后无法恢复!!!(回车确认删除。输入任何字符表示不删除):")
                    if tmp_search_list[0]["tmp_type"] == "student":
                        for i in range(len(student_list)):
                            if student_list[i]["number"] == tmp_search_list[0]["number"]:
                                if your_choice_4 == "":
                                    student_list.pop(i)
                                    print("删除成功!!!")
                                else:
                                    break
                    elif tmp_search_list[0]["tmp_type"] == "teacher":
                        for i in range(len(teacher_list)):
                            if teacher_list[i]["number"] == tmp_search_list[0]["number"]:
                                if your_choice_4 == "":
                                    teacher_list.pop(i)
                                    print("删除成功!!!")
                                else:
                                    break
                elif your_choice_3 == "3":
                    break

            elif len(tmp_search_list) >= 2:  # 针对如果查询出来多条信息
                your_choice_3 = input("请选择您需要对查询出来的信息的操作:1-->修改||2-->删除||3-->只是看看:")
                if your_choice_3 == "1":
                    while True:
                        your_choice_5 = input("请选择你要对那条信息做修改(输入序号):")
                        if not your_choice_5.isdecimal():
                            print("输入格式错误,请重新输入···")
                            continue
                        if int(your_choice_5) > len(tmp_search_list):
                            print("输入编号过大,请重新输入···")
                            continue
                        if int(your_choice_5) < 2:
                            print("输入编号过小,请重新输入···")
                        if tmp_search_list[(int(your_choice_5) - 1)]["tmp_type"] == "student":
                            for i in range(len(student_list)):
                                if student_list[i]["number"] == tmp_search_list[(int(your_choice_5) - 1)]["number"]:
                                    edit_name = input("请输入新的名字(直接回车不做修改):")
                                    if edit_name == "":
                                        print("名字未做修改!!")
                                    else:
                                        student_list[i]["name"] = edit_name
                                    edit_sex = input("请选择新的性别:1-->男||2-->女(直接回车不做修改):")
                                    if edit_sex == "1":
                                        student_list[i]["sex"] = "男"
                                    elif edit_sex == "2":
                                        student_list[i]["sex"] = "女"
                                    elif edit_sex == "":
                                        print("性别未做修改!!")
                                    edit_tel = input("请输入新的电话:(直接回车不做修改):")
                                    if edit_tel == "":
                                        print("电话为做修改!!")
                                    else:
                                        student_list[i]["tel"] = edit_tel
                                    print("修改完成!!!")
                            break
                        elif tmp_search_list[(int(your_choice_5) - 1)]["tmp_type"] == "teacher":
                            for i in range(len(teacher_list)):
                                if teacher_list[i]["number"] == tmp_search_list[(int(your_choice_5) - 1)]["number"]:
                                    edit_name = input("请输入新的名字(直接回车不做修改):")
                                    if edit_name == "":
                                        print("名字未做修改!!")
                                    else:
                                        teacher_list[i]["name"] = edit_name
                                    edit_sex = input("请选择新的性别:1-->男||2-->女(直接回车不做修改):")
                                    if edit_sex == "1":
                                        teacher_list[i]["sex"] = "男"
                                    elif edit_sex == "2":
                                        teacher_list[i]["sex"] = "女"
                                    elif edit_sex == "":
                                        print("性别未做修改!!")
                                    edit_tel = input("请输入新的电话:(直接回车不做修改):")
                                    if edit_tel == "":
                                        print("电话为做修改!!")
                                    else:
                                        teacher_list[i]["tel"] = edit_tel
                                    print("修改完成!!!")
                                break
                if your_choice_3 == "2":
                    while True:
                        your_choice_5 = input("请选择你要删除哪条信息(输入序号):")
                        if not your_choice_5.isdecimal():
                            print("输入格式错误,请重新输入···")
                            continue
                        if int(your_choice_5) > len(tmp_search_list):
                            print("输入编号过大,请重新输入···")
                            continue
                        if int(your_choice_5) < 2:
                            print("输入编号过小,请重新输入···")
                            continue
                        your_choice_4 = input("请确认是否要删除该条记录,删除后无法恢复!!!(回车确认删除。输入任何字符表示不删除):")
                        if tmp_search_list[(int(your_choice_5) - 1)]["tmp_type"] == "student":
                            for i in range(len(student_list)):
                                if student_list[i]["number"] == tmp_search_list[(int(your_choice_5) - 1)]["number"]:
                                    if your_choice_4 == "":
                                        student_list.pop(i)
                                        print("删除成功!!!")
                                    else:
                                        break
                            break

                        elif tmp_search_list[(int(your_choice_5) - 1)]["tmp_type"] == "teacher":
                            for i in range(len(teacher_list)):
                                if teacher_list[i]["number"] == tmp_search_list[(int(your_choice_5) - 1)]["number"]:
                                    if your_choice_4 == "":
                                        teacher_list.pop(i)
                                        print("删除成功!!!")
                                    else:
                                        break
                            break
                elif your_choice_3 == "3":
                    break

            break


class Teacher:

    def __init__(self):
        self.number = None
        self.name = None
        self.sex = None
        self.tel = None
        self.inside_num = None

    def add_teacher(self):
        self.number = 10000 + len(teacher_list)
        self.name = input("请输入你要增加的老师的姓名:")

        while True:
            self.sex = int(input("请输入老师的性别代码·1-->男||2-->女:"))
            if self.sex == 1:
                self.sex = "男"
                break
            elif self.sex == 2:
                self.sex = "女"
                break
            else:
                print("输入错误,请重新输入···")

        self.tel = input("请输入老师的电话:")
        self.inside_num = "tea_%d" % tea_num
        print("\r")
        print("以下是您添加的学生信息:", end="\r")
        print_head()
        print(self.inside_num, end=" " * 23)
        print(self.number, end=" " * 19)
        print(self.name, end=" " * (24 - len(self.name)))
        print(self.sex, end=" " * 22)
        print(self.tel)


class Student:

    def __init__(self):
        self.number = None
        self.name = None
        self.sex = None
        self.tel = None
        self.inside_num = None

    def add_student(self):
        self.number = 10000 + len(student_list)
        self.name = input("请输入你要增加的学生的姓名:")

        while True:
            self.sex = int(input("请输入学生的性别代码·1-->男||2-->女:"))
            if self.sex == 1:
                self.sex = "男"
                break
            elif self.sex == 2:
                self.sex = "女"
                break
            else:
                print("输入错误,请重新输入···")

        self.tel = input("请输入学生的电话:")
        self.inside_num = "stu_%d" % stu_num
        print("\r")
        print("以下是您添加的学生信息:", end="\r")
        print_head()
        print(self.inside_num, end=" "*23)
        print(self.number, end=" "*19)
        print(self.name, end=" "*(24-len(self.name)))
        print(self.sex, end=" "*22)
        print(self.tel)


main_code = 0
while True:
    show_head_table()
    your_choice = input("请选择操作功能:")
    if your_choice == '2':
        new_code = 0
        while new_code < 3:
            your_choice_1 = input("请选择你要新增的对象:1-->学生||2-->老师:")
            if your_choice_1 == "1":
                a = "stu_%d" % stu_num
                a = Student()
                a.add_student()
                stu_num += 1
                student_list.append(a.__dict__)
                break
            elif your_choice_1 == "2":
                a = "stu_%d" % tea_num
                a = Teacher()
                a.add_teacher()
                tea_num += 1
                teacher_list.append(a.__dict__)
                break
            else:
                print("您的输入错误,请重新输入···")
                new_code += 1
                if new_code == 3:
                    print("您连续输错了三次,我们得从头开始咯···")

    elif your_choice == "1":
        show_all_info()

    elif your_choice == "3":
        search()

    elif your_choice == "0":
        break

    else:
        main_code += 1
        if main_code == 3:
            print("您连续输错三次功能代码,我们不得不强制结束这个程序了···")
            break
        else:
            print("您选择的操作功能不正确,请重新选择呢···")



部分运行结果展示

1.添加

在这里插入图片描述

2.如果选择功能代码连续输错三次

在这里插入图片描述

3.显示全部

在这里插入图片描述

4. 查询功能

和之前的名片管理系统一样,删除和修改位于查询功能里面,这里不再做演示。
后期学习完整个python和前端开发之后,会把这个程序作为第一个网页开发实例~~

学海无涯!愿我们互相扶持,一起进步!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值