0
嗨。我正在嘗試創建一個能夠創建用戶然後登錄的程序。登錄後,您可以更改密碼。我似乎無法弄清楚的是如何在您登錄後使用舊密碼來更改密碼。我可以通過輸入您的帳戶名稱來實現它,但那不是重點。 做任何你有如何解決這個問題的任何想法,我願意接受建議:)使用json的賬戶管理
import json
with open("login_data.txt", "r") as login_file:
try:
users = json.load(login_file)
except:
users = {}
status = ""
def Display_Menu():
status = input("Are you a registered user? (y/n)? Press q to quit: ")
if status == "y":
Old_User()
elif status == "n":
New_User()
elif status == "passwd":
Change_Passwd()
elif status == "q":
skriva = open("login_data.txt", "w")
json.dump(users, skriva)
return status
def New_User():
Create_Login =input("Create login name: ")
if Create_Login in users:
print ("Login name already exist!")
else:
Create_Password =input("Create password: ")
users[Create_Login] = Create_Password
print("New User created!")
def Old_User():
login =input("Enter login name: ")
Password =input("Enter password: ")
if login in users and users[login] == Password:
print("Login successful!")
print(users[login])
status = input("Wanna quit, change pass, och logout?")
if status == "passwd":
Change_Passwd()
elif status == "logout":
Display_Menu()
elif status == "q":
skriva = open("login_data.txt", "w")
json.dump(users, skriva)
return status
else:
print("User doesn't exist or wrong password!")
def Change_Passwd():
oldpass =input("Old password: ")
if oldpass in users:
Create_Password =input("New password: ")
users[oldpass] = Create_Password
if Create_Password == input("Confirm password: "):
print("Password changed!")
else:
print("User authorization failure")
users[create_Login] = oldpass
else:
print ("No password match!")
while status != "q":
status = Display_Menu()