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()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值