学生信息管理系统(增删改查)

def meun():
    menu_info = '''+************************+
| 1)添加学生信息                                     |
| 2)显示所有学生的信息                          |
| 3)删除学生信息                                     |
| 4)修改学生信息                                     |
| 5)保存学生信息到文件(students.txt)     |
| 6)从文件中读取数据(students.txt)        |
| 退出:其他任意按键<回车>                   |
+*******************************************+
'''
    print(menu_info)
#|1)添加学生信息
def add_student_info():
    l=[]
    while True:
        name=input('请输入学生名字: ')
        if not name:
            break
        try:
            age=int(input('学生年龄: '))
            sex=input('性别: ')
            tel=int(input('联系电话:'))
        except:
            print('输入错误,请重新输入')
            continue
        lst={'name':name,'age':age,'sex':sex,'tel':tel}
        l.append(lst)
    return l

# |2)显示所有学生的信息
def show_student_info(student_info,del_name=''):
    if not student_info:
        pritn('无学生信息,返回菜单')
        return
        # center()返回一个原字符串居中, 并使用空格填充至长度width的新字符串。默认填充字符为空格
    print('姓名'.center(10), '年龄'.center(4), '性别'.center(4),'联系方式'.center(12))
    for i in student_info:
        print(i.get('name').center(10),str(i.get('age')).center(4),str(i.get('sex')).center(4),str(i.get('tel')).center(12))

# |3)删除学生信息
def del_student_info(student_info,del_name=''):
    if not del_name:
        del_name = input("请输入删除的学生姓名:")
    for info in student_info:
        if del_name == info.get("name"):
            return info
    raise IndexError("学生信息不匹配,没有找到%s" %del_name)

# |4)修改学生信息
def mod_student_info(student_info):
    mod_name=input('请输入要修改的学生名称:')
    for info in student_info:
        if info.get('name')==mod_name:
            mod_age=int(input('请输入学生年龄:'))
            mod_sex=input('请输入学生性别:')
            mod_tel=int(input('请输入学生联系电话:'))
            info={'name':mod_name,'age':mod_age,'sex':mod_sex,'tel':mod_tel,}
            return info
    raise IndexError('姓名不匹配:%s'%mod_name)

#用于年龄排序:
def get_age(*l):
    for x in l:
        return x.get('age')

from operator import itemgetter

# |5)保存学生信息到文件(students.txt)
def save_info(student_info):
    try:
        # 以写模式打开,并清空文件内容
        student_txt = open('student.txt','w')
    except Exception as e:
        # 文件不存在,创建文件并打开
        student_txt = open('student.txt','x')
        for info in student_info:
            student_txt.write(str(info)+'\n')
        student_txt,close()

# |6)从文件中读取数据
def read_info():
    old_info = []
    try:
        students_txt = open("students.txt")
    except:
        print("暂未保存数据信息")  # 打开失败,文件不存在说明没有数据保存
        return
    while True:
        info = students_txt.readline()
        if not info:
            break
        # print(info)
        info = info.rstrip() 
        # print(info)
        info = info[1:-1]  
        # print(info)
        student_dict = {} 
        for x in info.split(","): 
            # print(x)
            key_value = []  
            for k in x.split(":"):
                k = k.strip()  
                # print(k)
                if k[0] == k[-1] and len(k) > 2: 
                    key_value.append(k[1:-1]) 
                else:
                    key_value.append(int(k))
                # print(key_value)
            student_dict[key_value[0]] = key_value[1]  # 学生信息添加
        # print(student_dict)
        old_info.append(student_dict)  # 所有学生信息汇总
    students_txt.close()
    return old_info

def main():
    student_info=[]
    while True:
        meun()
        number=input('请输入您的选项: ')
        if number == '1':
            student_info+=add_student_info()

        elif number == '2':
            show_student_info(student_info)

        elif number == '3':
            try:
                student_info.remove(del_student_info(student_info))
            except Exception as e:
                print(e)
        elif number == '4':
            try:
                student=mod_student_info(student_info)
            except Exception as e:
                print(e)
            else:
                # 根据输入的名字,从列表删除该信息,再重新添加该学生最新信息
                student_info.remove(del_student_info(student_info, del_name=student.get("name")))
                student_info.append(student)

        elif number == '5':
            save_info(student_info)

        elif number == '6':
            student_info = read_info()
        else:
            break
        input('--回车返回菜单--')
main()

你可能感兴趣的:(学生信息管理系统(增删改查))