Python

day9作业2_登录注册(学生系统修改中)

2018-07-26  本文已影响20人  HavenYoung

登录注册

import file_funcs
import stu_sys

# 登录注册界面
def list_login():
    print('''
    +++++++++++++++++++++++++++++++++++++++++++++++++
    +        welcome to student manage system       +
    +                   1.register                  +
    +                                               +
    +                   2.login                     +
    +                                               +
    +                   0.exit                      +
    +++++++++++++++++++++++++++++++++++++++++++++++++
    ''')
    cmd = input('>>>>>>>')
    if '0'<=cmd<='2':
        return cmd
    else:
        print('输入内存错误!')
        return False


def login():
    while True:
        result = list_login()
        if not result:
            continue
        if result == '1':
            user_name = input('请输入用户名:')
            password = input('请输入密码:')
            users = file_funcs.get_infos('users')
            for index in range(len(users)):
                if user_name == users[index]['name']:
                    print('该用户名已经存在!')
                    break
            else:
                new_user = {'name':user_name, 'password':password, 'database':user_name+'_database'}
                file_funcs.add_info('users', new_user)
                print('注册成功!')
        if result == '2':
            user_name = input('请输入用户名:')
            password = input('请输入密码:')
            users = file_funcs.get_infos('users')
            for index in range(len(users)):
                if user_name == users[index]['name']:
                    if password == users[index]['password']:
                        print('登录成功!')
                        stu_sys.main(user_name+'_database')
                    else:
                        print('密码错误!')
                        break
            else:
                print('用户名不存在!')
        if result == '0':
            return

if __name__ == '__main__':
    login()

文件读写:

import json


# 获取信息
def get_infos(file_name):
    try:
        with open('./files/'+file_name+'.json', 'r', encoding='utf-8') as f:
            all_stu_infos = json.load(f)

    except:
        all_stu_infos = []

    return all_stu_infos


# 添加信息
def add_info(file_name,stu_info):
    # 获取之前的学生信息
    all_stu_infos = get_infos(file_name)
    with open('./files/'+file_name+'.json', 'w', encoding='utf-8') as f:
        # 将新的学生信息添加到所有学生信息的最后
        all_stu_infos.append(stu_info)
        # 保存所有信息
        json.dump(all_stu_infos, f)
        # 返回保存成功的信息
        return True


# 重新写入
def rewrite(file_name,stu_info):
    with open('./files/'+file_name+'.json', 'w', encoding='utf-8') as f:
        # 保存所有信息
        json.dump(stu_info, f)
        # 返回保存成功的信息
        return True

if __name__ == '__main__':
    pass

学生系统

import file_funcs


def list_main():
    print('''
        +++++++++++++++++++++++++++++++++++++++++++++++++
        +       welcome to student manage system        +
        +       1.add student information               +
        +       2.View personal information by id       +
        +       3.del personal information by id        +
        +       4.view all information                  +
        +       5.view personal average score by id     +
        +       0.exit                                  +
        +++++++++++++++++++++++++++++++++++++++++++++++++
            ''')
    user_cmd = input('>>>>>')
    return user_cmd


# 判断用户输入
def user_input(user_cmd):
    if user_cmd == '1':
        stu_info = stu_find()
        if not isinstance(stu_info, str):
            print('该学生信息已存在!')
            return
        name, stu_id, english, pe, art, math, age = stu_info_input(stu_info)
        stu_create(name, stu_id, english, pe, art, math, age)
        # 判断是否继续或者退出
        flag = '1'
        redo(flag)

    elif user_cmd == '2':
        stu_info = stu_find()
        if isinstance(stu_info, str):
            print('学生信息不存在!')
        else:
            # 打印学生信息
            print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % (stu_info['name'],
                stu_info['stu_id'],stu_info['score']['English'],stu_info['score']['PE'],
                stu_info['score']['art'],stu_info['score']['math'],stu_info['age']))
            # 判断是否继续或者退出
            flag = '2'
            redo(flag)

    elif user_cmd == '3':
        stu_info = stu_find()
        if isinstance(stu_info, str):
            print('学生信息不存在!')
        else:
            stu_del(stu_info)
            # 判断是否继续或者退出
            flag = '3'
            redo(flag)

    elif user_cmd == '4':
        stu_find_all()
        # 判断是否继续或者退出
        flag = '4'
        redo(flag)

    elif user_cmd == '5':
        stu_info = stu_find()
        if isinstance(stu_info, str):
            print('学生信息不存在!')
        else:
            score_ave(stu_info)
            # 判断是否继续或者退出
            flag = '5'
            redo(flag)

    elif user_cmd == '0':
        exit(0)
    else:
        print('输入内容有误!')




# 查找学生
def stu_find():
    stu_infos = file_funcs.get_infos(file_name)
    stu_info = input('请输入学生的学号:')
    for index_info in range(len(stu_infos)):
        # 学生信息存在
        if stu_info == stu_infos[index_info]['stu_id']:
            return stu_infos[index_info]
        # 学生信息不存在
    else:
        return stu_info



# 查找全部
def stu_find_all():
    stu_infos = file_funcs.get_infos(file_name)
    for index_info in range(len(stu_infos)):
        print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % (stu_infos[index_info]['name'],
                                                                     stu_infos[index_info]['stu_id'],
                                                                     stu_infos[index_info]['score']['English'],
                                                                     stu_infos[index_info]['score']['PE'],
                                                                     stu_infos[index_info]['score']['art'],
                                                                     stu_infos[index_info]['score']['math'],
                                                                     stu_infos[index_info]['age']))

# 用户输入学生信息
def stu_info_input(stu_info):
    stu_id = stu_info
    name = input('请输入学生的姓名:')
    english = input('请输入音乐成绩:')
    pe = input('请输入体育成绩:')
    art = input('请输入美术成绩:')
    math = input('请输入数学成绩:')
    age = input('请输入学生的年龄:')
    return name, stu_id, english, pe, art, math, age


# 添加学生信息
def stu_create(name, stu_id, english, pe, art, math, age):
    # stu_infos.append({'name':name, 'stu_id':stu_id,'score':{'English':english, 'PE':pe, 'art':art, 'math':math},
    #                   'age':age})
    file_funcs.add_info(file_name, {'name':name, 'stu_id':stu_id,'score':{'English':english, 'PE':pe, 'art':art, 'math':math},
                      'age':age})


# 删除学生信息
def stu_del(stu_info):
    stu_infos = file_funcs.get_infos(file_name)
    index_stu = stu_infos.index(stu_info)
    del stu_infos[index_stu]
    file_funcs.rewrite(file_name, stu_infos)
    print('删除信息成功!')


# 平均成绩
def score_ave(stu_info):
    score_average = (int(stu_info['score']['English']) + int(stu_info['score']['PE'])
                 + int(stu_info['score']['art']) + int(stu_info['score']['math'])) / 4

    print('姓名:%s\t学号:%s\t平均成绩是:%d' % (stu_info['name'],
                                      stu_info['stu_id'], score_average))


#
def redo(flag):
    if flag =='1':
        print('''
        1.继续添加
        2.返回上一层
        ''')
        user_cmd = input('>>>>>')
        if user_cmd == '1':
            user_input(flag)
        elif user_cmd == '2':
            return
        else:
            print('输入有误!')

    elif flag == '2':
        print('''
                1.继续查看
                2.返回上一层
                ''')
        user_cmd = input('>>>>>')
        if user_cmd == '1':
            user_input(flag)
        elif user_cmd == '2':
            return
        else:
            print('输入有误!')

    elif flag == '3':
        print('''
                1.继续删除
                2.返回上一层
                ''')
        user_cmd = input('>>>>>')
        if user_cmd == '1':
            user_input(flag)
        elif user_cmd == '2':
            return
        else:
            print('输入有误!')
    elif flag == '4':
        print('''
                1.继续查看
                2.返回上一层
                ''')
        user_cmd = input('>>>>>')
        if user_cmd == '1':
            user_input(flag)
        elif user_cmd == '2':
            return
        else:
            print('输入有误!')

    elif flag == '5':
        print('''
                1.继续查询
                2.返回上一层
                ''')
        user_cmd = input('>>>>>')
        if user_cmd == '1':
            user_input(flag)
        elif user_cmd == '2':
            return
        else:
            print('输入有误!')


# 主程序
def main(user_name):
    global file_name
    file_name = user_name
    global stu_infos
    stu_infos = file_funcs.get_infos(user_name)
    while True:
        # 打印列表内容,等待用户输入
        user_input(list_main())


# 开始程序
if __name__ == '__main__':
    main('joe_database')
上一篇下一篇

猜你喜欢

热点阅读