在我目前的代码中,我可以注册一个由电子邮件和密码组成的新帐户,登录并重置帐户密码。但我的代码有一个大问题:每当我想更改某个帐户的密码时,它会重写整个文件,删除现有的任何其他帐户,并将其替换为登录帐户的电子邮件和更改的密码。
# import all necessary packages to be used
import csv
from IPython.display import clear_output
# handle user registration and writing to csv
def registerUser( ):
with open("users.csv", mode="a", newline="") as f:
writer = csv.writer(f, delimiter=",")
print("To register, please enter your info:")
email = input("E-mail: ")
password = input("Password: ")
password2 = input("Re-type password: ")
clear_output( )
if password == password2:
writer.writerow( [email, password] )
print("You are now registered!")
else:
print("Something went wrong. Try again.")
def change_password( ):
with open("users.csv", mode="w", newline="") as f:
writer = csv.writer(f, delimiter=",")
email = input("Please type in your E-Mail: ")
newpassword = input("Type your new password: ")
newpassword2 = input("Re-type yor new password: ")
clear_output( )
if newpassword == newpassword2:
writer.writerow( [email, newpassword] )
print("You succesfully changed your password!")
else:
print("The password's don't match.")
# ask for user info and return true to login or false if incorrect info
def loginUser( ):
print("To login, please enter your info:")
email = input("E-mail: ")
password = input("Password: ")
clear_output( )
with open("users.csv", mode="r") as f:
reader = csv.reader(f, delimiter=",")
for row in reader:
if row == [email, password]:
print("You are now logged in!")
return True
print("Something went wrong, try again.")
return False
# variables for main loop
active = True
logged_in = False
# main loop
while active:
if logged_in:
print("1. Logout\n2. Quit\n3. Change password")
else:
print("1. Login\n2. Register\n3. Quit")
choice = input("What would you like to do? ").lower( )
clear_output( )
if choice == "register" and logged_in == False:
registerUser( )
elif choice == "login" and logged_in == False:
logged_in = loginUser( )
elif choice == "change password" and logged_in == True:
change_password( )
elif choice == "quit":
active = False
print("Thanks for using our software!")
elif choice == "logout" and logged_in == True:
logged_in = False
print("You are now logged out.")
else:
print("Sorry, please try again!")
我正在寻找一个解决方案,只取代已经登录的帐户的密码,不要求帐户的邮件,因为更改密码是唯一可能在我的代码时,用户已经登录。
1条答案
按热度按时间wz1wpwve1#
你找到解决的方法了吗?面对同样的问题,希望可以分享。