python创建一个字典、保存用户名和密码_如何创建一个Python字典来存储多个帐户的用户名和密码...

1586010002-jmsa.png

The problem I have right now is that for my dictionary that uses a key:value to store username:password is that every time I rerun the program, the current key:value is reset and the dictionary is set to empty again. The goal of my program is to have a person log in with a username and password and be able to store notes and passwords (I did this with python .txt files). Then the next person can come along, create an account and do the same. Here is my code (I have commented every line of code pertaining to my problem):

def userPass():

checkAccount = input("Do you have an account (Y or N)?")

if (checkAccount == 'N' or checkAccount == 'n'):

userName = input("Please Set Your New Username: ")

password = input("Please Set Your New Password: ")

// if (userName in dictAcc):

print("Username is taken")

userPass()

else:

// dictAcc[userName] = password

print("Congratulations! You have succesfully created an account!")

time.sleep(1.5)

dataInput()

elif(checkAccount == 'Y' or checkAccount == 'y'):

login()

else:

print("Invalid answer, try again")

userPass()

def login():

global userName

global password

global tries

loginUserName = input("Type in your Username: ")

loginPass = input("Type in your Password: ")

if (tries < 3):

// for key in dictAcc:

// if (loginUserName == key and loginPass == dictAcc[key]):

// print("You have successfully logged in!")

dataInput()

else:

print("Please try again")

tries += 1

login()

if (tries >= 3):

print("You have attempted to login too many times. Try again later.")

time.sleep(300)

login()

userPass()

解决方案

As others have mentioned, you need to have your dictionary saved into a file and load it when you restart your program. I adjusted your code to work for me and created two functions, one to save the dictionary (savedict) and another to load it (loaddict). The except IOError part is just so that it creates a new file if it doesn't exist.

Note that in general, storing passwords in a text file is a very bad idea. You can clearly see the reason why if you try to open the "dictAcc.txt" file (it will have all passwords there).

import pickle

import time

def loaddict():

try:

with open("dictAcc.txt", "rb") as pkf:

return pickle.load(pkf)

except IOError:

with open("dictAcc.txt", "w+") as pkf:

pickle.dump(dict(), pkf)

return dict()

def savedict(dictAcc):

with open("dictAcc.txt", "wb") as pkf:

pickle.dump(dictAcc, pkf)

def userPass():

dictAcc = loaddict() #Load the dict

checkAccount = raw_input("Do you have an account (Y or N)?")

if (checkAccount == 'N' or checkAccount == 'n'):

userName = raw_input("Please Set Your New Username: ")

password = raw_input("Please Set Your New Password: ")

if (userName in dictAcc):

print("Username is taken")

userPass()

else:

dictAcc[userName] = password

print("Congratulations! You have succesfully created an account!")

savedict(dictAcc) #Save the dict

time.sleep(1.5)

# dataInput() Code ends

elif(checkAccount == 'Y' or checkAccount == 'y'):

login()

else:

print("Invalid answer, try again")

userPass()

def login():

global userName

global password

global tries

loginUserName = raw_input("Type in your Username: ")

loginPass = raw_input("Type in your Password: ")

dictAcc = loaddict() #Load the dict

if (tries < 3):

for key in dictAcc:

if (loginUserName == key and loginPass == dictAcc[key]):

print("You have successfully logged in!")

# dataInput() Code ends

else:

print("Please try again")

tries += 1

login()

if (tries >= 3):

print("You have attempted to login too many times. Try again later.")

time.sleep(3)

tries=1 #To restart the tries counter

login()

global tries

tries=1

userPass()

你可能感兴趣的:(python创建一个字典,保存用户名和密码)