练习_登陆系统(未完善)

import os
Determine = False   #用于判定是否进入登陆成功后的一步操作
#登陆系统
def login_system():
    f = open('user.txt','r')
    info = f.readlines()
    f.close()
    count = 0 #用于记录输入次数
    flag = True  #用于整体循环
    global username  #定义全局用于后面文件查找登陆的用户是谁
    global password  #用于后面修改密码
    while flag:
        username = input('username:')
        flag1 = False   #作为能入继续下一步的标志
        flag2 = False   #作为是否登陆成功的标志
        for line in info:  #检查用户是否存在
            if username == line.split()[0]:
                flag1 = True
        if flag1 == False:
            print('用户不存在!')
            count += 1

        while flag1:   #验证密码是否存在
            password = input('password:')
            for line in info:
                if username == line.split()[0]:
                    if password == line.split()[1]:
                        flag2 = True
            if flag2 == False:
                print('密码错误!')
                count += 1
                flag1 = False
            elif flag2 == True:  #登陆成功后的操作
                flag = False
                flag1 = False
        if count == 3:
            print('输入错误达3次,退出程序!')
            exit()
    print('\n登陆成功!')
    global Determine
    Determine = True
def create_system():
    f = open('user.txt','r')
    info = f.readlines()  #用于验证用户名在文件中是否已经存在
    f.close()
    flag = True
    while flag:
        flag2 = 'continue'
        username = input('creat_username:')
        for line in info:
            if username == line.split()[0]:
                print('该用户已存在!')
                flag2 = 'Flase'
                break
        if flag2 == 'continue':
            password = input('your_password:')
            age = input('your_age:')
            position = input('your_position:')
            department= input('your_department:')
            f = open('user.txt','a')
            f.write('\n' + username)
            f.write(' ' + password + ' ' + age + ' ' + position + ' ' + department)
            f.close()
            flag = False
    main()
def modify():  #信息修改      !!!存在BUG   ,和密码修改的BUG  一样
    f_name = 'user.txt'
    f_new_name = '%s.new' % f_name
    f = open(f_name, 'r')
    f_new = open(f_new_name, 'w')
    info = f.readlines()
    f.seek(0)
    old_information = 'None'
    #使用while 循环  ,if判断输入编号,指定修改位置
    for information in info:
        if username == information.split()[0]:
            for index in enumerate(information.split()):
                print(index)
    choice = int(input('请输入你要修改的编号:'))
    for i in info:
        if username == i.split()[0]:
            old_information = i.split()[choice]
    new_information = input('input_your new information:')
    for g in f:
        if username == g.split()[0]:
            if old_information in g.split()[choice]:
                g.split()[choice] = g.replace(old_information,new_information)
        f_new.write(g)
    f.close()
    f_new.close()
    os.replace(f_new_name,f_name)
    print('信息修改成功!')
    main2()
def info_print():  #信息打印
    info2 = '''
 ——————INFORMATION————— 
|账号:%-29s|
|密码:%-29s|
|年龄:%-29s|
|职位:%-29s|
|职责:%-29s|
 —————————————————
'''
    f = open('user.txt','r')
    info = f.readlines()
    f.close()
    for information in info:
        if username == information.split()[0]:
            information_reserve = information.split()
            print('\n当前用户的信息如下:')
            # print('账号___密码___年龄___职位___职责')    #待优化__格式化输出!
            print(info2%(information_reserve[0],information_reserve[1],information_reserve[2],information_reserve[3],information_reserve[4]))
            # print(information)
    main2()
def chance_password(): #当前用户的密码修改         !!!!存在BUG,修改密码时不是在当前行 进行修改
    f_name = 'user.txt'
    f_new_name = '%s.new' % f_name
    f = open(f_name, 'r')
    f_new = open(f_new_name, 'w')
    old_password = password
    new_password = input('input_your new password:')
    for i in f:
        if username == i.split()[0]:
            if old_password in i:
                i = i.replace(old_password,new_password)
        f_new.write(i)
    f.close()
    f_new.close()
    os.replace(f_new_name,f_name)
    print('密码修改成功!')
    main2()
#return 最好不要放在while里面,容易出错!
def del_infomation():
    f_name = 'user.txt'
    f_new_name = '%s.new' % f_name
    f = open(f_name, 'r')
    f_new = open(f_new_name, 'w')
    before = f.readlines()
    new = ''
    f.seek(0)
    for information in before:
        if username == information.split()[0]:
            old = information
            for g in f:
                if username in g:
                    g = g.replace(old,new)
                f_new.write(g)
    f.close()
    f_new.close()
    os.replace(f_new_name,f_name)
    print('删除信息成功!')


info = '''
 —————请输入相关编号—————
|1、登陆系统                       |
|2、注册                           |
|0、退出系统                       |
 —————————————————
'''

info2 = '''
 —————请输入相关编号—————
|1、修改个人信息                   |
|2、打印个人信息                   |
|3、修改密码                       |
|4、删除当前用户账号               |
|0、退出系统                       |
 —————————————————
'''
flag_del_infomation = False
def main2():
    print(info2)
    flag = True
    global flag_del_infomation
    while flag:
        choice = input('请输入编号:')
        if choice == '1':
            modify()
            break
        elif choice == '2':
            info_print()
            break
        elif choice == '3':
            chance_password()
            break
        elif choice == '4':
            del_infomation()
            flag_del_infomation = True
            break
        elif choice == '0':
            print('程序结束,谢谢使用!')
            exit()
        else:
            print('输入有误,请重新输入!\n')
def main():
    print(info)
    flag = True
    while flag:
        choice = input('请输入编号:')
        if choice == '1':
            login_system()
            break
        elif choice == '2':
            create_system()
            break
        elif choice == '0':
            flag = False
        else:
            print('输入有误,请重新输入!\n')
main()
if Determine == True:
    main2()
else:
    print('程序结束,感谢您的使用!')
if flag_del_infomation:
    main()
    if Determine == True:
        main2()
    else:
        print('程序结束,感谢您的使用!')

猜你喜欢

转载自blog.csdn.net/gjtzbq/article/details/81142890