我剛剛開始學習Python編程,我正在嘗試創(chuàng)建一個登錄系統(tǒng),該系統(tǒng)使用終端并將數(shù)據(jù)存儲到JSON文件中并檢索它以登錄。這是導致問題的部件。def login(): login_username = input("Enter your username: ") login_password = input("Enter your pass: ") with open("data.json", 'r+') as data: if login_username == username and login_password == password: print("Successful Login") else: print("Please Try Again") login()當我被要求輸入用戶和傳遞并且輸入與JSON中的數(shù)據(jù)匹配時,它將循環(huán)并要求一次又一次地輸入用戶名和密碼。我的整個代碼在下面import jsonuname = ""password =""def register(): uname = input("Enter a user to log in with: ") password = input("Enter a password: ") confirmed_pass = input("Enter the above password again") if password != confirmed_pass: print("Both the passwords does not match please re-enter a pass") password = input("Enter a password:") confirmed_pass = input("Enter the above password again") login_info = { "Username": uname, "Password": password } with open("data.json", "w") as write_file: json.dump(login_info, write_file, separators=(',', ':')) log = input("Would you like to login? (Y/N)") if log == 'Y' or log == "y": login() else: quit()def login(): login_uname = input("Enter your username: ") login_password = input("Enter your pass: ") with open("data.json", 'r') as data: if login_uname == uname and login_password == password: print("Successful Login") else: print("Please Try Again") login()reg = input("Have you registered (Y/N) ?: ")if reg == 'Y' or reg == 'y': login()elif reg == 'N' or reg == 'n': register()else: print("Error!")
1 回答

慕蓋茨4494581
TA貢獻1850條經(jīng)驗 獲得超11個贊
根據(jù)您的代碼,您應該首先使用json.load將數(shù)據(jù)加載到python字典中:
with open("data.json", 'r') as data: login = json.loads(data.read()) # then you can use it like this if login_username == login['Username'] and login_password == login['Password']: ... Rest of the code
只是一個頭,您的登錄名僅適用于最后一個注冊用戶,您應該解決這個問題。
添加回答
舉報
0/150
提交
取消