当前位置 博文首页 > nsc07的博客:老师,学生,管理员

    nsc07的博客:老师,学生,管理员

    作者:[db:作者] 时间:2021-09-22 22:39

    '''选课系统'''
    student_list = []
    teacher_list = []
    class_list = []
    
    
    '''学生端'''
    def read_all_student():
        if student_list:
            return
        with open('学生信息.txt', 'rt', encoding='utf-8')as f:
            text = f.read()
            if not text:
                return
            texts = text.split(',')
            for i in texts:
                temp = i.split('|')
                d_dic = {}
                d_dic['name'] = temp[0]
                d_dic['password'] = temp[1]
                d_dic['xuan_de_ke'] = temp[2]
                student_list.append(d_dic)
    
    
    def student_exist(name):
        for student in student_list:
            if student['name'] == name:
                return True
        return False
    
    
    def write_user(name, password):  # xuan_de_ke
        with open('students.txt', 'a+', encoding='utf-8')as f:
            old = ',%s|%s|%s' % (name, password, [])
            f.write(old)
    
    
    def student_register():  # 注册
        while True:
            name = input('请输入注册的用户名(按0退出):').strip()
            if name == '0':
                return
            password = input('请输入注册的密码:').strip()
            # xuan_de_ke = [0]
            if name and password:
                print('ok')
                if student_exist(name):
                    print('用户名已存在')
                    continue
                else:
                    print('可以注册')
                    if len(password) < 6:
                        print('密码不能少于6位')
                        continue
                    else:
                        print('注册成功')
                        write_user(name, password)  # , xuan_de_ke
                        student_list.append({'name': name, 'password': password, 'xuan_de_ke': []})
                        return
            else:
                print('用户名或密码不能为空')
    
    
    student_login = {}
    
    
    def student_login_er():  # 登陆
        while True:
            name = input('输入用户名(按0返回上层):')
            if name == '0':
                return
            flag = False
            for stu in student_list:
                if stu['name'] == name:
                    flag = True
                    break
            else:
                print('用户不存在,请先注册')
                continue
            password = input('请输入密码:')
            for stu in student_list:
                if stu['name'] == name and stu['password'] == password:
                    print('登陆成功,欢迎%s' % name)
                    student_login['name'] = name
                    student_login['password'] = password
                    student_login['xuan_de_ke'] = []
                    return True
    
    def read_all_class():
        if student_list:
            return
        with open('class_now.txt', 'rt', encoding='utf-8')as f:
            text = f.read()
            if not text:
                return
            texts = text.split(',')
            for i in texts:
                temp = i.split('|')
                d_dic = {}
                d_dic['name'] = temp[0]
                d_dic['time'] = temp[1]
                d_dic['teacher'] = temp[2]
                d_dic['students'] = temp[2]
                class_list.append(d_dic)
    
    
    # 修改从文件中获取课程
    def choose_class():  # 选课
        read_all_class()
        if not student_list:
            print('请先注册或登陆')
            return
        while True:
            for i in class_list:
                print('课程{},{}'.format(i[0], i[1]))
            num = input('请输入选择的课程(输入0退出):')
            if num == '0':
                print('已选择的课程%s' % student_login['xuan_de_ke'])
                return
            for i in class_list:
                if num not in i:
                    continue
                else:
                    student_login['xuan_de_ke'].append(i)
                    for i in student_list:
                        if student_login['name'] == i['name']:
                            i['xuan_de_ke'].append(i)
    
    
    def look_chosen_class():
        if student_login:
            print(student_login['xuan_de_ke'])
    
    
    def student():
        read_all_student()
        menu = {'1': student_register,
                '2': student_login,
                '3': choose_class,
                '4': look_chosen_class
                }
        while True:
            print('''----您已进入学生端,请选择您需要的功能----
        1.注册
        2.登录
        3.选课
        4.查看已选课程
        0.退出''')
            choice = input('请输入选择序号(按0返回上层):').strip()
            if choice == '0':
                return
            if choice not in menu:
                print('输入有误,请重新输入')
                continue
            menu[choice]()
    
    '''老师'''
    
    
    def read_all_teacher():
        if teacher_list:
            return
        with open('老师信息.txt', 'rt', encoding='utf-8')as f:
            text = f.read()
            if not text:
                return
            texts = text.split(',')
            for i in texts:
                temp = i.split('|')
                d_dic = {}
                d_dic['name'] = temp[0]
                d_dic['password'] = temp[1]
                # d_dic['xuan_de_ke'] = temp[2]
                teacher_list.append(d_dic)
    
    
    def teacher_exist(name):
        for tea in teacher_list:
            if tea['name'] == name:
                return True
        return False
    
    
    def write_user2(name, password):
        with open('老师信息.txt', 'a+', encoding='utf-8')as f:
            old = ',%s|%s' % (name, password)
            f.write(old)
    
    
    teacher_login = {}
    
    
    def teacher_login_er(): #  登陆
        while