017-python函数和常用模块-函数式编程实现登陆和注册
1 def login(username, password): 2 """ 3 用于用户登录 4 :param username: 用户输入的用户名 5 :param password: 用户输入的密码 6 :return: true,表示登录成功;false,登录失败 7 """ 8 f = open("db", 'r') 9 for line in f: 10 line_list = line.strip().split("|") 11 if line_list[0] == username and line_list[1] == password: 12 return True 13 return False 14 15 16 def register(username, password): 17 """ 18 用于用户注册 19 :param username: 用户名 20 :param password: 密码 21 :return: 默认None 22 """ 23 f = open("db", 'a') 24 temp = "\n" + username + "|" + password 25 f.write(temp) 26 f.close() 27 28 29 def main(): 30 t = input("1:登录;2:注册") 31 if t == "1": 32 user = input("请输入用户名:") 33 pwd = input("请输入密码:") 34 r = login(user, pwd) 35 if r: 36 print("登录成功") 37 else: 38 print("登录失败") 39 elif t == "2": 40 user = input("请输入用户名:") 41 pwd = input("请输入密码:") 42 register(user, pwd) 43 44 main()