1 # -*- coding:utf-8 -*- 2 def login(username, password): 3 """ 4 用于用户登陆 5 :param username: 用户名 6 :param password: 密码 7 :return: 8 """ 9 f = open(‘text‘, ‘r‘) 10 for line in f: 11 line_list = line.strip().split(‘ ‘) 12 if line_list[0] == username and line_list[1] == password: 13 return True 14 elif line_list[0] != username or line_list[1] != password: 15 return 0 16 return False 17 18 19 def register(): 20 """ 21 用于用户注册 22 :return: 23 """ 24 user = input("请输入您的用户名:") 25 pwd = input("请输入您的密码:") 26 pwd_2 = input("请再次输入您的密码:") 27 if pwd != pwd_2: 28 print("第二次输入的密码和第一次不同,请重新注册!\n") 29 register() 30 f = open(‘text‘, ‘a‘) 31 temp = ‘\n‘ + user + ‘ ‘ + pwd 32 f.write(temp) 33 f.close() 34 print("注册成功!") 35 36 37 def main(): 38 choice = input("1、登陆 2、注册\n") 39 if choice == ‘1‘: 40 user = input("请输入您的用户名:") 41 pwd = input("请输入您的密码:") 42 result = login(user, pwd) 43 if result: 44 print("登陆成功!") 45 elif result == ‘0‘: 46 print("用户名或密码错误!") 47 else: 48 print("用户名和密码均错误!") 49 if choice == ‘2‘: 50 register() 51 52 main()
时间: 2024-11-10 11:24:07