python学生信息管理系统(函数封装)

page = '''
==============================
❀❀欢迎大佬:

    ❤1.添加学生
    ❤2.查看学生
    ❤3.修改学生信息
    ❤4.删除学生
    ❤5.退出系统
==============================
'''
student_information = [
    { '学号':'stu_1','姓名': '张三', '年龄': 12, '电话': 1234},
    { '学号':'stu_5','姓名': '李四', '年龄': 13, '电话': 1245},
    { '学号':'stu_22','姓名': '王麻子', '年龄': 13, '电话':1547},
]
#对已有的信息进行处理
need_id = student_information[-1]['学号']
new_str = ''
for i in need_id:
    if i.isdigit():
        new_str += i
int_new_str = int(new_str) # 将学号后的字符串中的数字转换成可执行的数字

def school_num():
    '''
    生成学号
    '''
    num = int_new_str
    while True:
        num += 1
        yield num

re = school_num()




page1 = '''
==============================
        ❤1.继续添加
        ❤2.返回上一级
==============================
 '''


page2 ='''
===========================
        ❀1.查看所有学生
        ❀2.按姓名查找
        ❀3.按学号查找
        ❀4.返回上一级
===========================
'''


def add_(**kwargs):
    '''
    将输入的学生信息添加到student_information
    '''
    student_information.append(kwargs)
    return kwargs


def shou_ye():
    print(page)
    #打印主界面
    global num
    num = int(input('请选择(1-5):'))
    #给出选择

def obtain_element():
    '''
    显示当前添加学生的信息
    '''
    for key in add_student:
        print(key + ':', add_student[key], end=' ')


def view_all():
    '''
    查看所有学生信息
    :return:
    '''
    for k in range(len(student_information)):
        for key1 in student_information[k]:
            print(key1 + ':',student_information[k][key1],end="   ")
        print()

def view_names(list_all,names:str):
    '''
    显示对应姓名所对应的信息
    '''
    flag = 0
    for k in range(len(student_information)):
        for key1 in student_information[k]:
            if student_information[k][key1] == name1:
                dict1 = student_information[k]
                for i in dict1:
                    print(i +":",dict1[i],end=' ')
                flag = 1
    if flag == 0:
        print('查无此人')

def view_xue_hao(list_all,xue_hao:int):
    '''
    按照学号查看学生信息
    '''
    flag = 0
    for k in range(len(student_information)):
        for key1 in student_information[k]:
            if student_information[k][key1] == 'stu_'+str(xue_hao):
                dict1 = student_information[k]
                for i in dict1:
                    print(i + ":", dict1[i], end=' ')
                flag = 1
    if flag == 0:
        print('查无此人')


def mod_information(list_all,xue_hao:int):
    '''
    按学号更新学生信息
    '''
    flag = 0
    for k in range(len(student_information)):
        for key1 in student_information[k]:
            if student_information[k][key1] == 'stu_'+str(xue_hao):
                name = input('更新的名字为:')
                age = int(input('更新的年龄为:'))
                tel = int(input('更新的电话为:'))
                global update_information
                update_information = student_information[k].update({'姓名':name,'年龄':age,'电话':tel})
                dict1 = student_information[k]
                for i in dict1:
                    print(i + ":", dict1[i], end=' ')
            flag = 1
    if flag == 0:
        print('查无此人,请输入正确的学号')

page3 = '''
==============================
        ❤1.继续修改
        ❤2.返回上一级
==============================
'''

page4 = '''
==============================
        ❤1.按姓名删除
        ❤2.按学号删除
        ❤3.返回主菜单
==============================
'''
def del_names(list_all,name):
    '''
    按姓名删除学生信息
    '''
    student_index = 0
    flag = 0
    while student_index < len(student_information):
        for key1 in student_information[student_index]:
            if student_information[student_index][key1] == name:
                del student_information[student_index]
                print('删除成功!')
                flag = 1
        else:
            student_index += 1
    if flag == 0:
        print('查无此人,请输入正确的姓名')

def del_xue_hao(list_all,xue_hao:int):
    '''
    按学号删除学生信息
    '''
    student_num = 0
    flag = 0
    while student_num < len(student_information):
        for key1 in student_information[student_num]:
            if student_information[student_num][key1] == 'stu_'+str(xue_hao):
                del student_information[student_num]
                print('删除成功!')
                flag = 1
        else:
            student_num += 1
    if flag == 0:
        print('查无此人,请输入正确的学号')




shou_ye()
while num < 6:

    if num == 1:
        print('''==============================
        ❤1.添加学生
        ❤2.返回上一级
==============================''')
        num5 = int(input('请选择1-2:'))
        while num5 != 2:
            if num5 == 1:
                add_student = add_(学号='stu_%.3d' % (next(re)), 姓名=input('输入添加学生姓名'), 年龄=input('输入添加学生年龄'),
                                   电话=input('输入添加学生电话'))
                print('添加成功')
                obtain_element()
                print(page1)
                num1 = int(input('请选择1-2:'))
                while num1 != 2:
                    if num1 == 1:
                        add_student = add_(学号='stu_%.3d' % (next(re)), 姓名=input('输入添加学生姓名'), 年龄=input('输入添加学生年龄'),
                                           电话=input('输入添加学生电话'))
                        print('添加成功')
                        obtain_element()
                        print(page1)
                        num1 = int(input('请选择1-2:'))
                    else:
                        shou_ye()
                break
        else:
            shou_ye()



    elif num == 2:
            print(page2)
            num2 = int(input('请选择1-4:'))
            while num2 != 4:
                if num2 == 1:
                    view_all()
                    a = input('按回车键继续')
                elif num2 == 2:
                    name1 =input('请输入姓名;')
                    view_names(student_information,name1)
                    a = input('按回车键继续')

                elif num2 == 3:
                    xue_hao = int(input('请输入学号;'))
                    view_xue_hao(student_information,xue_hao)
                    a = input('按回车键继续')

                else:
                    print('输入不规范,请重新输入')
                    a = input('按回车键继续')
                break

            else:
                shou_ye()
    elif num == 3:
        mod_id = int(input('请输入学号更新信息:'))
        mod_information(student_information,mod_id)
        b = input('按回车键继续')
        print(page3)
        num3 = int(input('请选择1-2:'))
        while num3 != 2:
            if num3 == 1:
                mod_id = int(input('请输入学号更新信息:'))
                mod_information(student_information, mod_id)
                b = input('按回车键继续')
                print(page3)
                num3 = int(input('请选择1-2:'))
            else:
                print('输入不规范,请重新输入')
                a = input('按回车键继续')
                break
        else:
            shou_ye()
    elif num == 4:
        print(page4)
        num4 = int(input('请选择1-3:'))
        while num4 != 3:
            if num4 ==1:
                del_name = input('请输入姓名删除信息:')
                del_names(student_information,del_name)
                a = input('按回车键继续')
            elif num4 == 2:
                xue_hao1 = int(input('请输入学号删除信息:'))
                del_xue_hao(student_information,xue_hao1)
                a = input('按回车键继续')
            break
        else:
            shou_ye()

    elif num == 5:
           print('退出成功!')
           break
    else:
        print('请输入正确的数字')
        shou_ye()
        break

你可能感兴趣的:(python学生信息管理系统(函数封装))