day10-作业

学生信息管理系统——文件操作

import  json
students = []
while True:
    # print('学生信息管理系统'.center(30, '*'))
    # print('1、添加学生信息'.center(30, ' '))
    # print('2、查看学生信息'.center(30, ' '))
    # print('3、修改学生信息'.center(30, ' '))
    # print('4、删除学生信息'.center(30, ' '))
    # print('5、退出系统'.center(30, ' '))
    # print('**************************************')


    with open ('./files/default.txt', 'r', encoding= 'utf-8') as f:
        print(f.read())
    student = (dict.fromkeys(['name', 'age', 'tel']))
    caozuo = int(input('请选择你要进行的操作:'))
    if caozuo == 1:
        print('学生信息管理系统'.center(30, '*'))
        print('添加学生信息'.center(30, ' '))
        student['name'] = input('请输入学生姓名:')
        student['age'] = int(input('请输入学生年龄:'))
        student['tel'] = input('请输入学生电话:')
        with open('./files/user.json', 'r', encoding='utf-8') as f:
            students = json.load(f)
            students.append(student)
        with open('./files/user.json', 'w', encoding='utf-8') as f:
            # f.write(str(student))
            json.dump(students,f)
        print('添加成功')
        # print(students)
        print('**************************************')
    elif caozuo == 2:
        print('学生信息管理系统'.center(30, '*'))
        print('查看学生信息'.center(30, ' '))
        # print(students)
        with open('./files/user.json', 'r', encoding='utf-8') as f:
            print(json.load(f))
        print('**************************************')
    elif caozuo == 3:
        print('学生信息管理系统'.center(30, '*'))
        print('修改学生信息'.center(30, ' '))
        stu_name = input('请输入学生姓名:')
        with open('./files/user.json', 'r', encoding='utf-8') as f:
            stu_inf = json.load(f)
        for stu in stu_inf:
            if stu['name'] == stu_name:
                print(stu)
                value = input('是否要修改?Y/N')
                if value == 'N':
                    continue
                stu['age'] = int(input('请修改学生年龄:'))
                stu['tel'] = input('请修改学生电话:')
        with open('./files/user.json', 'w', encoding='utf-8') as f:
                json.dump(stu_inf, f)
        print('修改成功')
        print('**************************************')
    elif caozuo == 4:
        print('学生信息管理系统'.center(30, '*'))
        print('删除学生信息'.center(30, ' '))
        stu_name = input('请输入学生姓名:')
        with open('./files/user.json', 'r', encoding='utf-8') as f:
            stu_inf = json.load(f)
        for stu in stu_inf:
            if stu['name'] == stu_name:
                print(stu)
                value = input('是否想删除该学生?Y/N')
                if value == 'N':
                    continue
                stu_inf.remove(stu)
        with open('./files/user.json', 'w', encoding='utf-8') as f:
            json.dump(stu_inf,f)
        print('删除成功')
        print('**************************************')
    elif caozuo == 5:
        break

登录界面

import  json
students = []
while True:

    with open ('./files/login', 'r', encoding= 'utf-8') as f:
        print(f.read())
    student = (dict.fromkeys(['name', 'password']))
    caozuo = int(input('请选择你要进行的操作:'))
    if caozuo == 2:
        print('学生信息管理系统'.center(30, '*'))
        print('注册'.center(30, ' '))
        student['name'] = input('请输入用户名:')
        student['password'] = input('请输入密码:')
        with open('./files/login.json', 'r', encoding='utf-8') as f:
            students = json.load(f)
            students.append(student)
        with open('./files/login.json', 'w', encoding='utf-8') as f:
            json.dump(students,f)
        print('注册成功')
        print('**************************************')
    elif caozuo == 1:
        print('学生信息管理系统'.center(30, '*'))
        print('登录'.center(30, ' '))
        stu_name = input('请输入学生用户名:')
        stu_password = input('请输入密码:')
        with open('./files/login.json', 'r', encoding='utf-8') as f:
            stu_inf = json.load(f)
        for stu in stu_inf:
            if stu['name'] == stu_name and stu['password'] == stu_password:
                print('登录成功')
                import student_system
            else:
                print('用户名或密码错误!')
        print('**************************************')
    elif caozuo == 3:
        break

你可能感兴趣的:(day10-作业)