用户登录管理
1 # -*- coding:utf-8 -*- 2 login_user = {'is_login': False} 3 4 5 def register(): 6 user = input('请输入您的用户名:') 7 file = open('text','r',encoding='utf-8') 8 for line in file: 9 line_list = line.strip().split(' ') 10 if line_list[0] == user: 11 print('该用户已存在,请使用其它用户名进行注册!') 12 register() 13 register2(user) 14 15 16 def register2(user): 17 pwd = input('请输入您的密码:') 18 pwd_2 = input('请再次输入您的密码:') 19 if pwd != pwd_2: 20 print('两次密码输入不一致,请重新输入!') 21 register2(user) 22 register3(user, pwd) 23 24 25 def register3(user, pwd): 26 f = open('text','a',encoding='utf-8') 27 f.write('\n' + user + ' ' + pwd) 28 f.close() 29 print('注册成功!') 30 main() 31 32 33 def login(): 34 user = input('请输入用户名:') 35 pwd = input('请输入密码:') 36 f = open('text','r') 37 lines = f.readlines() 38 for line in lines: 39 line_list = line.strip().split(' ') 40 if line_list[0] == user and line_list[1] == pwd: 41 login_user['current_user'] = user 42 login_user['current_pwd'] = pwd 43 return True 44 return False 45 46 47 def manager(): 48 if login_user['is_login']: 49 print('欢迎%s登陆!' % login_user['current_user']) 50 else: 51 print('请先登录!') 52 main() 53 54 55 def changepwd(): 56 if login_user['is_login']: 57 login_user['new_pwd'] = input('请输入新密码:') 58 delete() 59 print('密码修改成功!') 60 else: 61 print('请先登录!') 62 main() 63 64 65 def delete(): 66 lines = open('text','r').readlines() 67 for i in range(len(lines)): 68 if login_user['current_user'] in lines[i]: 69 lines[i] = lines[i].replace(login_user['current_user'] + ' ' + login_user['current_pwd'],login_user['current_user'] + ' ' + login_user['new_pwd']) 70 open('text','w').writelines(lines) 71 72 73 def main(): 74 while True: 75 print('【1】.登陆 【2】.注册 【3】后台管理 【4】修改密码') 76 choice = input('请输入序号进行登陆或注册:') 77 if choice == '1': 78 ret = login() 79 if ret: 80 print("登陆成功!") 81 login_user['is_login'] = True 82 else: 83 print("用户名或密码错误,请重新登陆!") 84 if choice == '2': 85 register() 86 if choice == '3': 87 manager() 88 if choice == '4': 89 changepwd() 90 91 main()