学生管理系统

"""
__author__ = 叶兵
"""
import time
import json


def student_system(account_name):

    def number():
        all_student_list = read_file()
        numb = len(all_student_list['student']) + 1
        while True:
            yield "python1902" + str(numb).rjust(3, '0')
            numb += 1

    gen = number()

    def read_file():
        with open('student.json', encoding='utf-8') as f:
            all_student_temp = json.loads(f.read(), encoding='utf-8')
            return all_student_temp

    def write_file(all_student_list):
        with open('student.json', 'w', encoding='utf-8') as f:
            temp = json.dumps(all_student_list)
            f.write(temp)

    first_temp = True
    while first_temp:
        def welcome():
            global num
            print('================================')
            print('欢迎%s:' % account_name)
            print('     1.  添加学生')
            print('     2.  查看学生')
            print('     3.  修改学生信息')
            print('     4.  删除学生')
            print('     5.  返回(退出登录)')
            print('================================')
            num = input('请选择(1-5):')
            return manage_system(num)

        def manage_system(num):
            if num == '1':
                add_temp = True
                while add_temp:
                    all_student = []
                    name1 = input('请输入学生姓名:')
                    age1 = int(input('请输入学生年龄:'))
                    tel1 = input('请输入学生电话:')

                    def add_student(name: str, age: int, tel: str):
                        all_student.append({'id': next(gen), 'name': name, 'age': age, 'tel': tel})
                        print('添加成功!')

                        # return all_student

                    add_student(name1, age1, tel1)
                    # 先读文件提出以前的数据,再添加新数据后,保存所有数据
                    all_student_temp = read_file()
                    all_student_temp["student"].append(all_student[0])
                    write_file(all_student_temp)

                    print('1. 继续')
                    print('2. 返回')
                    add_num = input('请选择(1-2):')
                    if add_num == '1':
                        continue
                    else:
                        add_temp = False
                        welcome()
            elif num == '2':
                find_temp = True
                while find_temp:
                    print('1. 查看所有学生')
                    print('2. 按姓名查找')
                    print('3. 按学号查找')
                    print('4. 返回')
                    find_num = input('请选择(1-4):')
                    if find_num == '1':
                        all_student_temp = read_file()
                        if len(all_student_temp["student"]) == 0:
                            print('数据库里面还没有存储学生')
                        else:
                            for stu in all_student_temp["student"]:
                                print(stu)
                            else:
                                print('1. 返回主页面:')
                                print('按任意键返回上一级页面:')
                                find_temp_num = input('请选择:')
                            if find_temp_num == '1':
                                find_temp =False
                                welcome()
                    elif find_num == '2':
                        find_num2_temp = True
                        while find_num2_temp:
                            student_name = input('请输入学生姓名:')
                            find_name_student = []
                            all_student_temp = read_file()
                            for index in range(len(all_student_temp["student"])):
                                if all_student_temp["student"][index]['name'] == student_name:
                                    find_name_student.append(all_student_temp["student"][index])
                            if len(find_name_student) == 0:
                                print('没有找到该学生信息')
                                print('1. 返回主页面:')
                                print('按任意键返回上一级页面:')
                                find_num3 = input('请选择:')
                                if find_num3 == '1':
                                    find_num2_temp = find_temp = False
                                    welcome()
                                else:
                                    continue
                            else:
                                for stu in find_name_student:
                                    print(stu)
                                find_num2_temp = False
                                print('1. 返回主页面:')
                                print('按任意键返回上一级页面:')
                                # find_temp_num = input('请选择:')
                            if input('请选择:') == '1':
                                find_temp = False
                                welcome()
                    elif find_num == '3':
                        id_temp = True
                        while id_temp:
                            find_id = input('请输入学生学号:')
                            all_student_temp = read_file()
                            for index in range(len(all_student_temp["student"])):
                                if all_student_temp["student"][index]['id'] == find_id:
                                    print(all_student_temp["student"][index])
                                    id_temp = False
                                    break
                            else:
                                print('没有找到该学生信息')
                    else:
                        welcome()
            elif num == '3':
                print('1. 修改名字:')
                print('2. 修改年龄:')
                print('3. 修改电话:')
                print('4. 返回')
                modify_num = input('请选择(1-3):')
                modify_temp = True
                while modify_temp:
                    your_id = input('请输入你的学号:')
                    all_student_temp = read_file()
                    if modify_num == '1':
                        for index in range(len(all_student_temp["student"])):
                            if all_student_temp["student"][index]['id'] == your_id:
                                print('您要修改的学生信息为', all_student_temp["student"][index])
                                modify_name = input('请输入你要修改成的名字:')
                                all_student_temp["student"][index]['name'] = modify_name
                                print('修改之后为:', all_student_temp["student"][index])
                                write_file(all_student_temp)
                                modify_temp = False
                                break
                        else:
                            print('抱歉!没有找到和您对应学号的学生,请检查您输入的学号是否正确。')
                            print('1. 回到主页面查询学号信息')
                            print('2. 继续操作,重新输入学号')
                            modify_num2 = input('请选择(1-2):')
                            if modify_num2 == '1':
                                welcome()
                    elif modify_num == '2':
                        for index in range(len(all_student_temp["student"])):
                            if all_student_temp["student"][index]['id'] == your_id:
                                print('您要修改的学生信息为', all_student_temp["student"][index])
                                modify_age = input('请输入你要修改成的年龄:')
                                all_student_temp["student"][index]['age'] = modify_age
                                print('修改之后为:', all_student_temp["student"][index])
                                write_file(all_student_temp)
                                modify_temp = False
                                break
                        else:
                            print('抱歉!没有找到和您对应学号的学生,请检查您输入的学号是否正确。')
                            print('1. 回到主页面查询学号信息')
                            print('2. 继续操作,重新输入学号')
                            modify_num2 = input('请选择(1-2):')
                            if modify_num2 == '1':
                                welcome()
                    elif modify_num == '3':
                        for index in range(len(all_student_temp["student"])):
                            if all_student_temp["student"][index]['id'] == your_id:
                                print('您要修改的学生信息为', all_student_temp["student"][index])
                                modify_tel = input('请输入你要修改成的电话:')
                                all_student_temp["student"][index]['tel'] = modify_tel
                                print('修改之后为:', all_student_temp["student"][index])
                                write_file(all_student_temp)
                                modify_temp = False
                                break
                        else:
                            print('抱歉!没有找到和您对应学号的学生,请检查您输入的学号是否正确。')
                            print('1. 回到主页面查询学号信息')
                            print('2. 继续操作,重新输入学号')
                            modify_num2 = input('请选择(1-2):')
                            if modify_num2 == '1':
                                welcome()
                    else:
                        welcome()
                        break
            elif num == '4':
                remove_temp = True
                all_student_temp = read_file()
                while remove_temp:
                    print('请慎重考虑要删除的学生:')
                    remove_id = input('请输入你要删除学生的学号:')
                    for index in range(len(all_student_temp["student"])):
                        if all_student_temp["student"][index]['id'] == remove_id:
                            print('你要删除的学生信息如下,请仔细核对后再删除!')
                            print(all_student_temp["student"][index])
                            print('1. 确认删除')
                            print('2. 返回(或任意键返回)')
                            confirm_num = input('请选择(1-2):')
                            if confirm_num == '1':
                                del all_student_temp["student"][index]
                                print('删除成功')
                                write_file(all_student_temp)
                                time.sleep(1)
                                remove_temp = False
                                break
                            else:
                                remove_temp = False
                                break
                    else:
                        print('没有找到该学生信息!请重新输入,或者返回主页面进行学号查询。')
                        print('1. 重新输入学号')
                        print('2. 返回主页面进行查询')
                        remove_num = input('请选择(1-2):')
                        if remove_num == '1':
                            continue
                        else:
                            remove_temp = False
                            welcome()
            else:
                regist()

        welcome()


def regist():
    while True:
        def true_regist(account):
            with open('account.json', 'w') as f:
                f.write(json.dumps(account))
                print('注册成功')
                time.sleep(1)

        def enter(account_name):
            student_system(account_name)

        print('====================')
        print('欢迎使用学生管理系统')
        print('====================')
        print('1. 注册')
        print('2. 登录')
        print('====================')
        regist_num = input('请选择(1-2):')
        if regist_num == '1':
            account_number = input('请输入账号:')
            with open('account.json', encoding='utf-8') as f:
                account = json.loads(f.read(), encoding='utf-8')
                if account == {}:
                    password = input('请输入密码:')
                    account[account_number] = password
                    true_regist(account)

                for key in account:
                    if key == account_number:
                        print('该账号已经被注册,注册失败')
                        print('====================')
                        time.sleep(1)
                        break
                else:
                    password = input('请输入密码:')
                    account[account_number] = password
                    true_regist(account)

        else:
            with open('account.json', encoding='utf-8') as f:
                account_temp = json.loads(f.read(), encoding='utf-8')
                your_account = input('请输入账号:')
                if your_account not in account_temp.keys():
                    print('您输入的账号没有注册,1秒后回到主页面')
                    print('====================')
                    time.sleep(1)
                else:
                    temp_pass = True
                    while temp_pass:
                        your_password = input('请输入密码:')
                        if account_temp[your_account] == your_password:
                            print('登录成功,请稍后...')
                            time.sleep(1)
                            temp_pass = False
                            enter(your_account)
                        else:
                            print('您输入的密码错误!!!!!')


regist()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354

推荐阅读更多精彩内容