利用python实现简单登陆注册系统
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 def login(username,password): 6 ''' 7 8 :param username:用户名 9 :param password:密码 10 :return: True:登陆成功 False:登陆失败 11 ''' 12 f = open('log','r',encoding='utf-8') 13 for line in f: 14 line = line.strip() 15 line_list = line.split('$') 16 if username ==line_list[0] and password ==line_list[1]: 17 print('success') 18 return True 19 return False 20 21 def register(username,password): 22 ''' 23 24 :param username: 25 :param password: 26 :return: 27 ''' 28 with open('log','a',encoding='utf-8') as f: 29 temp = '\n' + username + '$' + password 30 f.write(temp) 31 return True 32 33 def user_exit(username): 34 ''' 35 36 :param username: 37 :return: 38 ''' 39 with open('log','r',encoding='utf-8') as f: 40 for line in f: 41 line = line.strip() 42 line_list = line.split('$') 43 if line_list[0]==username: 44 return True 45 return False 46 47 def main(): 48 print('欢迎登陆XX系统') 49 inp = input('1:登陆;2:注册') 50 user = input('user:') 51 pwd = input('pwd:') 52 if inp =='1': 53 is_login = login(user,pwd) 54 if is_login: 55 print('success') 56 else: 57 print('fail') 58 elif inp =='2': 59 is_exit = user_exit(user) 60 if is_exit: 61 print('already register') 62 else: 63 result = register(user,pwd) 64 if result: 65 print('register sucess') 66 else: 67 print('resgister faild') 68 main()