Python基础 - 学生管理系统

all_student = []


def create_id():
    for x in range(9999):
        yield x + 1


id = create_id()


def select_add():
    n = input('请输入:')
    if n == '1':
        add_student()
    elif n == '2':
        menu()
    else:
        print('输入有误!重新输入')
        select_add()


def add_student():
    print('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓')
    print('┃            添加学生信息           ┃')
    print('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛')
    class_name = 'py1807'
    name = input('姓名:')
    age = input('年龄:')
    tel = input('电话:')
    stuid = next(id)
    new_id = str(stuid).rjust(4, '0')
    stu_id = class_name + new_id
    student = {'name': name, 'age': age, 'tel': tel, 'stu_id': stu_id}
    all_student.append(student)
    print('1.继续添加')
    print('2.返回菜单')
    select_add()


def query_student():
    print('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓')
    print('┃            查看学生信息           ┃')
    print('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛')
    print('1.查看所有学生信息')
    print('2.按姓名查找')
    print('3.按学号查找')
    print('4.返回')
    n = input('请输入:')

    def query_all():
        if all_student:
            for student in all_student:
                print(student)
            print('')
        else:
            print('没有学生信息')
        print('')
        print('1.返回上层')
        print('2.返回主菜单')
        n = input('请输入:')
        if n == '1':
            query_student()
        elif n == '2':
            menu()
        else:
            print('输入有误')
            query_all()

    def query_name(name):
        count = 0
        for index in range(len(all_student)):
            if all_student[index]['name'] == name:
                print(all_student[index])
                count += 1
        if not count:
            print('没有信息')
            print('')
        print('1.返回上层')
        print('2.返回主菜单')
        n = input('请输入:')
        if n == '1':
            query_student()
        elif n == '2':
            menu()
        else:
            print('输入有误')
            query_name()

    def query_id(stuid):
        if all_student:
            count = 0
            for index in range(len(all_student)):
                if all_student[index]['stu_id'] == stuid:
                    print(all_student[index])
                    count += 1
                    break
            if count == 0:
                print('没有此学号对应的学生!')
        else:
            print('没有信息')
            print('')
        print('1.返回上层')
        print('2.返回主菜单')
        n = input('请输入:')
        if n == '1':
            query_student()
        elif n == '2':
            menu()
        else:
            print('输入有误')
            query_id()
    if n == '1':
        query_all()
    elif n == '2':
        name = input('输入要查找的姓名:')
        query_name(name)
    elif n == '3':
        stuid = input('输入要查找的学号:')
        query_id(stuid)
    elif n == '4':
        menu()
    else:
        print('输入有误,重新输入')
        query_student()


def del_student():
    print('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓')
    print('┃            删除学生信息           ┃')
    print('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛')
    print('1.按学号删除')
    print('2.按姓名删除')
    print('3.返回')

    def del_student_id():
        if all_student:
            stuid = input('输入你要删除的学生学号:')
            count = 0
            for student in all_student:
                if student['stu_id'] == stuid:
                    all_student.remove(student)
                    print('删除成功!')
                    count += 1
                    break
            if not count:
                print('没有对应学号的学生!')
            del_student()
        else:
            print('没有学生信息')
            del_student()

    def del_student_name():
        if all_student:
            name = input('输入你要删除的学生的姓名:')
            count = 0
            for student in all_student:
                if student['name'] == name:
                    count += 1
            if count == 1:
                for student in all_student:
                    if student['name'] == name:
                        all_student.remove(student)
                        print('删除成功!')
                        break
            elif count > 1:
                name_student = []
                num = 1
                for student in all_student:
                    if student['name'] == name:
                        student['no'] = num
                        name_student.append(student)
                        num += 1
                print('姓名', name, '的学生信息:')
                print(name_student)
                num2 = int(input('输入要删除的序号:'))
                for index in name_student:
                    if index['no'] == num2:
                        for student in all_student:
                            if student['stu_id'] == index['stu_id']:
                                all_student.remove(student)
                                print('删除成功!')
                                break
            elif count == 0:
                print('没有你要删除的姓名对应的学生!')
            del_student()
        else:
            print('没有学生信息')
            del_student()
    n = input('请输入:')
    if n == '1':
        del_student_id()
    elif n == '2':
        del_student_name()
    elif n == '3':
        menu()
    else:
        print('输入有误!')
        del_student()


def alter_student():
    print('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓')
    print('┃            修改学生信息           ┃')
    print('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛')
    print('1.按学号修改')
    print('2.返回')

    def alter_student_id():
        stuid = input('请输入要修改的学生的学号')
        print(stuid)
        for student in all_student:
            if student['stu_id'] == stuid:
                print('将要修改的学生信息:')
                print(student)

                def sele_item():
                    print('修改项:')
                    print('1.姓名')
                    print('2.年龄')
                    print('3.电话')
                    print('4.取消修改')
                    item = input('请输入你想要修改的项:')
                    if item == '1':
                        new_name = input('新的姓名:')
                        print(new_name)
                        student['name'] = new_name
                        print('修改姓名成功')
                        return
                    elif item == '2':
                        new_age = input('新的年龄:')
                        print(new_age)
                        student['age'] = new_age
                        print('修改年龄成功')
                        return
                    elif item == '3':
                        new_tel = input('新的电话:')
                        print(new_tel)
                        student['tel'] = new_tel
                        print('修改电话成功')
                        return
                    elif item == '4':
                        alter_student()
                    else:
                        print('输入有误!')
                        sele_item()

                sele_item()
            else:
                print('没有此学号的学生!')

        alter_student()
    n = input('请输入')
    if n == '1':
        alter_student_id()
    elif n == '2':
        menu()
    else:
        print('输入有误!')
        alter_student()


def menu():
    print('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓')
    print('┃                                               ┃')
    print('┃             欢迎进入学生信息管理系统              ┃')
    print('┃                                               ┃')
    print('┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫')
    print('┃                1.添加学生信息                   ┃')
    print('┃                2.修改学生信息                   ┃')
    print('┃                3.删除学生信息                   ┃')
    print('┃                4.查找学生信息                   ┃')
    print('┃                5.退出管理系统                   ┃')
    print('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛')
    n = input('请输入:')
    if n == '1':
        add_student()
    elif n == '2':
        alter_student()
    elif n == '3':
        del_student()
    elif n == '4':
        query_student()
    elif n == '5':
        print('谢谢使用!')
        return


menu()

你可能感兴趣的:(Python基础 - 学生管理系统)