def login(username,password): ''' 用於用戶登陸 :param username: 用戶輸入用戶名 :param password: 用戶輸入密碼 :return: True表示登錄成功 ''' f = open("db","r") for line in f:#一行一行檢測 line_list = line.strip().split("|") if line_list[0] == username and line_list[1] == password: return True def register(username,password): ''' 用戶註冊 :param username: 用戶名 :param password: 密碼 :return: 默認返回None ''' f = open("db", "a") temp = "\n" + username + "|" +password f.write(temp) f.close() def main(): t = input("1:登錄 2:註冊") if t == "1": user = input("請輸入用戶名:") pwd = input("請輸入密碼:") r = login(user,pwd) if r: print("登錄成功") else: print("登錄失敗") elif t == '2': user = input("請輸入用戶名:") pwd = input("請輸入密碼:") register(user,pwd) main()