学生管理系统2.5版本——含登录系统

目前存在问题:未实现用户权限划分和数据的合法性检测
main:

import function
all_students = []
while True:
    print('================================')
    print('1.❤登录')
    print('2.❤注册')
    print('3.❤返回')
    print('================================')
    log1 = input('请选择:')
    if log1 == '2':
        function.register()
    elif log1 == '1':
        with open('./user.txt', 'r', encoding='utf-8') as users:
            usernameslist1 = users.read().split(',')
        del usernameslist1[-1]
        usernameslist2 = []
        for i in usernameslist1:
            usernameslist2.append(i.split(':'))
            usernameslist3 = dict(usernameslist2)
        username2 = input('请输入用户名(6位以上):')
        password2 = input('请输入密码(6-8位):')
        if function.login(usernameslist3,username2,password2):
            while True:
                function.interface1(username2)
                contrl1 = input('请选择(1-5):')
                if contrl1 == '1':
                    function.wg_input(all_students)
                elif contrl1 == '2':
                    while True:
                        print('================================')
                        print('1.❤查看所有学生')
                        print('2.❤按姓名查找')
                        print('3.❤按学号查找')
                        print('4.❤返回')
                        sel = input('请选择(1-4):')
                        print('================================')
                        function.wg_sel(all_students, sel)
                        if sel == '4':
                            break
                elif contrl1 == '3':
                    while True:
                        print('================================')
                        print('1.❤按姓名查找修改')
                        print('2.❤按学号查找修改')
                        print('3.❤返回')
                        upda = input('请选择:(1-3):')
                        print('================================')
                        function.wg_update(all_students, upda)
                        if upda == '3':
                            break
                elif contrl1 == '4':
                    while True:
                        print('================================')
                        print('1.❤按学号查找删除')
                        print('2.❤按姓名查找')
                        print('3.❤返回')
                        del1 = input('请选择(1-3):')
                        print('================================')
                        function.wg_del(all_students, del1)
                        if del1 == '3':
                            break
                elif contrl1 == '5':
                    break
                else:
                    print('输入错误,请重新输入:')
                    continue
        else:
            print('账号密码有误,请重新输入')

    elif log1 =='3':
        break
    else:
        print('输入错误!请重新输入!')
        continue

function:

def log():
    print('================================')
    username = input('请输入用户名:')
    password = input('请输入密码:')
    print('================================')
    if username =='wangguo' and password=='123456':
        return True

def interface1(username):
    print('================================')
    print('❀❀欢迎您:%s'%username )
    print('1.❤添加学生')
    print('2.❤查看学生')
    print('3.❤修改学生信息')
    print('4.❤删除学生')
    print('5.❤返回')
    print('================================')
def wg_input(all_students:list):
    def get_num():
        num = 1
        while True:
            yield  '1%03.d' % (num)
            num += 1
    num_gen = get_num()
    while True:
            print('================================')
            addname = input('请输入姓名:')
            addage = int(input('请输入年龄:'))
            addscore = int(input('请输入成绩:'))
            addtel = input('请输入电话:')
            all_students.append(
            {'name': addname, 'num': next(num_gen), 'age': addage, 'score': addscore, 'tel': addtel})
            print('添加成功')
            print('1.❤继续添加')
            print('2.❤退出')
            contrl2 = input('请选择(1-2):')
            print('================================')
            if contrl2 == '1':
                continue
            else:
                break
def wg_sel(all_students:list,sel):
        if sel == '1':
            for item1 in all_students:
                print(item1)
        elif sel == '2':
            name1 = input('请输入您要查询的姓名:')
            i = 0
            list1 = []
            while i in range(len(all_students)):
                if all_students[i]['name'] == name1:
                    list1.append(all_students[i])
                i += 1
            if len(list1) == 0:
                print('没有找到该记录!!')
            else:
                for n in list1:
                    print(n)
        elif sel == '3':
            list2 = []
            num = input('请输入您要查询的学号:')
            for i in all_students:
                if num == i['num']:
                    list2.append(i)
            if len(list2) == 0:
                print('没有找到该记录!!')
            else:
                for n in list2:
                    print(n)
def wg_update(all_students:list,upda):
    if upda == '1':
        upname = input('请输入您需要修改的姓名:')
        for i in all_students:
            if i['name'] == upname:
                print('================================')
                print('1.❤修改姓名')
                print('2.❤修改学号')
                print('3.❤修改年龄')
                print('4.❤修改成绩')
                print('5.❤修改电话')
                print('6.❤返回')
                print('================================')
                upname1 = input('请输入您需要修改的信息:')
                if upname1 == '1':
                    upname2 = input('请输入修改后的姓名:')
                    i['name'] = upname2
                    print('修改成功')
                elif upname1 == '2':
                    upnum2 = input('请输入修改后的学号:')
                    i['num'] = upnum2
                    print('修改成功')
                elif upname1 == '3':
                    upage2 = input('请输入修改后的年龄:')
                    i['age'] = upage2
                    print('修改成功')
                elif upname1 == '4':
                    upscore2 = input('请输入修改后的成绩:')
                    i['score'] = upscore2
                    print('修改成功')
                elif upname1 == '5':
                    uptel2 = input('请输入修改后的电话:')
                    i['tel'] = uptel2
                    print('修改成功')
                elif upname1 == '6':
                    break
    if upda == '2':
        upnum = input('请输入您需要修改的学号:')
        for i in all_students:
            if i['num'] == upnum:
                print('================================')
                print('1.❤修改姓名')
                print('2.❤修改学号')
                print('3.❤修改年龄')
                print('4.❤修改电话')
                print('5.❤修改成绩')
                print('6.❤返回')
                upname1 = input('请输入您需要修改的信息:')
                print('================================')
                if upname1 == '1':
                    upname2 = input('请输入修改后的姓名:')
                    i['name'] = upname2
                    print('修改成功')
                elif upname1 == '2':
                    upnum2 = input('请输入修改后的学号:')
                    i['num'] = upnum2
                    print('修改成功')
                elif upname1 == '3':
                    upage2 = input('请输入修改后的年龄:')
                    i['age'] = upage2
                    print('修改成功')
                elif upname1 == '4':
                    upscore2 = input('请输入修改后的成绩:')
                    i['score'] = upscore2
                    print('修改成功')
                elif upname1 == '5':
                    uptel2 = input('请输入修改后的电话:')
                    i['tel'] = uptel2
                    print('修改成功')
                elif upname1 == '6':
                    break
def wg_del(all_students:list,del1):
    if del1 == '1':
        del2 = input('请输入您需要删除的学号:')
        list2 = []
        for i in all_students[:]:
            if del2 == i['num']:
                list2.append(i)
        if len(list2) == 0:
            print('没有找到该记录!!')
        else:
            print('确定删除吗?')
            print('1.确定')
            print('2.返回')
            sure = input('请选择:')
            if sure == '1':
                for i in all_students[:]:
                    if del2 == i['num']:
                        all_students.remove(i)
                        print('删除成功!')
                        print(all_students)
                    else:
                        break
    if del1 == '2':
        name1 = input('请输入您要查询的姓名:')
        i = 0
        list1 = []
        while i in range(len(all_students)):
            if all_students[i]['name'] == name1:
                list1.append(all_students[i])
            i += 1
        if len(list1) == 0:
            print('没有找到该记录!!')
        else:
            for n in range(len(list1)):
                print(n,list1[n])
            sure = int(input('请选择你所需要删除的序号:'))
            all_students.remove(list1[sure])
            print('删除成功!')
def register():
    while True:
        username = input('请输入用户名:(6位以上)')
        if 6 <= len(username):
            while True:
                password = input('请输入密码(6-8位):')
                if 6 <= len(password) <= 8:
                    with open('./user.txt', 'a', encoding='utf-8') as users:
                        users.write('%s:' % username)
                        users.write('%s,' % password)
                        print('注册成功!')
                    break
                else:
                    print('输入有误!请重新输入!')
                    continue
            break
        else:
            print('输入有误!请重新输入!')
            continue
def login(usernameslist3:dict,username2,password2):
        if 6 <= len(username2):
                if 6 <= len(password2) <= 8:
                    names = []
                    for n in usernameslist3:
                        names.append(n)
                        if n == username2 and password2 == usernameslist3[n]:
                            return True
                        elif n == username2 and password2 != usernameslist3[n]:
                            print('密码有误,请重新登录!')
                            return False

你可能感兴趣的:(学生管理系统2.5版本——含登录系统)