访问Python字典元素
我想将字典中的原始输入元素保存到变量中。以下是我正在做的一个示例:访问Python字典元素,python,dictionary,Python,Dictionary,我想将字典中的原始输入元素保存到变量中。以下是我正在做的一个示例: accounts = {} def accountcreater(): accountname = raw_input("Account Name: ") accountpassword = raw_input("Account Password: ") accountUUID = 1 accountUUID += 1 accounts[accountname] = {"password":accountp
accounts = {}
def accountcreater():
accountname = raw_input("Account Name: ")
accountpassword = raw_input("Account Password: ")
accountUUID = 1
accountUUID += 1
accounts[accountname] = {"password":accountpassword,"UUID":accountUUID}
def login():
loginusername = raw_input("Account Name: ")
loginpassword = raw_input("Account Password: ")
for usernames in account:
if usernames == loginusername:
accountpassword = accounts[usernames][???]
accountpassword = accounts[usernames][???]
else:
pass
这是一个非常简单的代码示例。现在“[??]”所在的部分我不知道该放什么。我试着把这个代码:
accountpassword = accounts[usernames][password]
accountpassword = accounts[usernames][UUID]
但这似乎不起作用,因为它说,password
和UUID
没有定义。然而,我似乎只需要输入[用户名]
,它就可以正常工作了。有什么想法吗
编辑
有关以下代码:
accountpassword = accounts[usernames]['password']
accountpassword = accounts[usernames]['UUID']
我还尝试过将它们放入字符串中,这会引发以下错误:字符串索引必须是整数,而不是str
编辑2
这是我的全部代码,请注意它是非常长和广泛的。您需要的唯一部分将位于函数startup、login和account的顶部
import datetime
import time
#import pickle
filesfile = "filesfiles" #File's Pickle File
accountfile = "accountsfiles" #Account's Pickle File
accounts = {} #Where accounts are put
files = {} #Where files are put
currentaccount = None #The current account the user is on
#accountsaver = open(accountfile,'r') #Restores all current accounts
#accounts = pickle.load(accountsaver)
#accountsaver.close()
#filesaver = open(filesfile,'r') #Restores all current files
#files = pickle.load(filesaver)
#filesaver.close()
def startup():
for accountname in accounts:
# accountpassword = accounts[accountname]['password']
print type(accountname)
#accountsaver = open(accountfile,'wb') #Adds a new account if there is one
#pickle.dump(accounts, accountsaver)
#accountsaver.close()
print "\n ------------------- "
print " FILE SYSTEM MANAGER "
print " ------------------- "
print "\n To login type in: LOGIN"
print " To create a new account type in: ACCOUNT"
loginornew = raw_input("\n Please enter LOGIN or ACCOUNT: ") #Input to see where you want to go
if loginornew.lower() == "login":
login()
elif loginornew.lower() == "account":
newaccount()
else:
startup()
def newaccount():
newusername = ""
newpassword = ""
newpasswordagain = ""
UUID = 0 #UUID variable
print "\n--------------------------------------------"
print "\n Would you like to create a new account?"
yesorno = raw_input("\n Please enter YES or NO: ") #Checks to see if user wants to create a new account
if yesorno.lower() == "no":
print "\n--------------------------------------------"
startup()
elif yesorno.lower() == "yes":
while len(newusername) < 8: #Checks to see if username is atleast 8 characters
newusername = raw_input("\n Username must be atleast 8 characters\n Please enter a username for your account: ")
while len(newpassword) < 5: #Checks to see if password is atleast 5 characters
newpassword = raw_input("\n Password must be atleast 5 characters\n Please enter a password for your account: ")
while newpasswordagain == "": #Makes sure there is a input
newpasswordagain = raw_input(" Please confirm the password for your account: ")
if newpassword == newpasswordagain: #Checks to make sure the password is correct
for username in accounts: #Loops through all usernames in acccounts
if username.lower() == newusername.lower(): #Checks to see if the username already exists
print "\n Username already exists"
print " Please try again"
newaccount()
else: #If the username is not taken and the password is correct it creates the accounts
pass
UUID += 1 #Makes the current UUID number bigger by one
accounts[newusername] = {"password":newpassword,"UUID":UUID} #Creates a new account
print "\n Account Created"
print "\n--------------------------------------------"
startup() #Takes you back to startup menu
else: #If the passwords do not match each other
print "\n Passwords do not match"
print " Please try again"
newaccount()
else:
newaccount()
def login():
username = ""
password = ""
print "\n--------------------------------------------"
print "\n Would you like to login?"
yesorno = raw_input("\n Please enter YES or NO: ") #Checks to see if user wants to login
if yesorno.lower() == "no":
print "\n--------------------------------------------"
startup()
elif yesorno.lower() == "yes":
for usernames2 in accounts: #Testing Purposes
print usernames2, #Testing Purposes
print "" #Testing Purposes
for usernames23 in accounts: #Testing Purposes
for usernames3 in str(accounts[usernames23]): #Testing Purposes
print usernames3, #Testing Purposes
while username == "": #Makes sure there is a input
username = raw_input("\n Please enter your username: ")
while password == "": #Makes sure there is a input
password = raw_input("\n Please enter your password: ")
for usernames in accounts: #Loops through all usernames in accounts
if username.lower() == usernames.lower(): #Checks to see if the username input equalls a username in the accounts dictionary
accountpassword = accounts['username'][password]
accountUUID = 0
if password == accountpassword:
for accountname in accounts:
accountpassword = accounts[accountname]
print "\n Access Granted"
print "\n--------------------------------------------"
menu()
else:
pass
print "\n Access Denied"
print "\n Please try again"
login()
else:
pass
print "\n Access Denied"
print "\n Please try again"
login()
else:
login()
def menu():
#filesaver = open(filesfile,'wb') #Adds a new file if there is one
#pickle.dump(files, filesaver)
#filesaver.close()
print "\n ------------------- "
print " FILE SYSTEM MANAGER "
print " ------------------- "
print "\n What would you like to do with your files?"
print " To make a new file type in: NEW"
print " To edit a current file type in: EDIT"
print " To delete a current file type in: DELETE"
print " To view all current files type in: ALL"
chooser = raw_input("\n Please enter NEW, EDIT, DELETE, or ALL: ") #Input to see where you want to go
if chooser.lower() == "new":
newfile()
elif chooser.lower() == "edit":
editfiles()
elif chooser.lower() == "delete":
deletefiles()
elif chooser.lower() == "all":
allfiles()
else:
menu()
def newfile():
filename = ""
filetext = ""
while filename == "": #Makes sure there is a input
print "--------------------------------------------"
filename = raw_input("\n Please input your new files name: ")
while filetext == "":
filetext = raw_input("\n Please input the text for your new file: ")
filedate = datetime.date.today() #Grabs the current date
files[filename] = {userUUID:{filedate:filetext}} #Creates a new file
print "\n File Added"
print "\n--------------------------------------------"
menu()
def editfiles():
print "--------------------------------------------"
print " To edit a file type in: EDIT"
print " To view all current files type in: ALLFILES"
print " To cancel type in: CANCEL"
wheretogo = raw_input("\n Please enter EDIT, ALLFILES, or CANCEL: ")
if wheretogo.lower() == "edit":
print "\n To edit a file type in its name"
print " To cancel type in: CANCEL"
print "\n **Please Note** Editing a file changes its date!"
editname = raw_input("\n Please type in the file's name or CANCEL: ")
if editname.lower() == "cancel":
menu()
else:
newcontents = ""
for filename in files: #Loops through all file names in files
if filename.lower() == editname.lower():
print "\n What would you like this file to say?"
while newcontents == "":
newcontents = raw_input("\n Please input files new contents: ")
filetext = newcontents
filedate = datetime.date.today()
files[filename] = {filedate:filetext}
print "\n File Changed"
print "--------------------------------------------"
menu()
else:
pass
print "\n File not found!"
editfiles()
elif wheretogo.lower() == "allfiles":
print "\n--------------------------------------------"
for filename in files:
print "File Name: " + str(filename)
print "--------------------------------------------"
print "\n To edit a file type in: EDIT"
print " To cancel type in: CANCEL"
print "\n **Please Note** Editing a file changes its date!"
wheretogo = raw_input("\n Please enter EDIT or CANCEL: ")
if wheretogo.lower() == "edit":
editname = raw_input("\n Please type in the file's name to edit it: ")
newcontents = ""
for filename in files:
if filename.lower() == editname.lower():
print "\n What would you like this file to say?"
while newcontents == "":
newcontents = raw_input("\n Please input files new contents: ")
filetext = newcontents
filedate = datetime.date.today()
files[filename] = {filedate:filetext}
print "\n File Changed"
print "--------------------------------------------"
menu()
else:
pass
print "\nFile not found!"
editfiles()
elif wheretogo.lower() == "cancel":
menu()
else:
menu()
elif wheretogo.lower() == "cancel":
menu()
else:
menu()
def deletefiles():
print "--------------------------------------------"
print " To delete a file type in: DELETE"
print " To view all current files type in: ALLFILES"
print " To cancel type in: CANCEL"
wheretogo = raw_input("\n Please enter DELETE, ALLFILES, or CANCEL: ")
if wheretogo.lower() == "delete":
print "\n To delete a file type in its name"
print " To cancel type in: CANCEL"
deletename = raw_input("\n Please type in the file's name or CANCEL: ")
if deletename.lower() == "cancel":
menu()
else:
for filename in files:
if filename.lower() == deletename.lower():
del files[filename]
print "\n File Removed"
print "--------------------------------------------"
menu()
else:
pass
print "\n File not found!"
deletefiles()
elif wheretogo.lower() == "allfiles":
print "\n--------------------------------------------"
for filename in files:
print "File Name: " + str(filename)
print "--------------------------------------------"
print "\n To delete a file type in: DELETE"
print " To cancel type in: CANCEL"
wheretogo = raw_input("\n Please enter DELETE or CANCEL: ")
if wheretogo.lower() == "delete":
deletename = raw_input("\n Please type in the file's name to delete it: ")
for filename in files:
if filename.lower() == deletename.lower():
del files[filename]
print "\n File Removed"
print "--------------------------------------------"
menu()
else:
pass
print "\nFile not found!"
deletefiles()
elif wheretogo.lower() == "cancel":
menu()
else:
menu()
elif wheretogo.lower() == "cancel":
menu()
else:
menu()
def allfiles():
filetexttotal = ""
for filename in files:
print "\n--------------------------------------------"
print "\nFile Name: " + str(filename)
for filedate in files[filename]:
print "File Date: " + str(filedate)
for filetext in files[filename][filedate]:
filetexttotal = filetexttotal + str(filetext)
print "File Contents: " + str(filetexttotal)
filetexttotal = ""
print "\n--------------------------------------------"
menu()
startup()
导入日期时间
导入时间
#进口泡菜
filefile=“filesfiles”#文件的Pickle文件
accountfile=“accountsfiles”#帐户的Pickle文件
accounts={}#放置帐户的位置
files={}#放置文件的位置
currentaccount=None#用户所在的当前帐户
#accountsaver=open(accountfile,'r')#恢复所有当前帐户
#accounts=pickle.load(accountsaver)
#accountsaver.close()
#filesaver=open(filesfile,'r')#恢复所有当前文件
#files=pickle.load(filesaver)
#filesaver.close()
def startup():
对于帐户中的accountname:
#accountpassword=帐户[accountname]['password']
打印类型(帐户名称)
#accountsaver=open(accountfile,'wb')#如果有新帐户,则添加新帐户
#pickle.dump(帐户、accountsaver)
#accountsaver.close()
打印“\n------------------”
打印“文件系统管理器”
打印“----------------------”
打印“\n要登录,请键入:登录”
打印“在:帐户中创建新帐户类型”
loginornew=raw_input(“\n请输入LOGIN或ACCOUNT:”)#输入以查看您想去哪里
如果loginornew.lower()=“登录”:
登录()
elif loginornew.lower()=“帐户”:
新帐户()
其他:
启动()
def newaccount():
newusername=“”
newpassword=“”
NewPasswordReach=“”
UUID=0#UUID变量
打印“\n-------------------------------------------”
打印“\n是否要创建新帐户?”
yesorno=raw_input(“\n请输入YES或NO:”)#检查用户是否要创建新帐户
如果yesorno.lower()=“否”:
打印“\n-------------------------------------------”
启动()
elif yesorno.lower()=“是”:
而len(newusername)<8:#检查用户名是否至少为8个字符
newusername=原始输入(“\n用户名必须至少包含8个字符\n请为您的帐户输入用户名:”)
而len(newpassword)<5:#检查密码是否至少为5个字符
newpassword=raw\u input(“\n密码必须至少包含5个字符\n请为您的帐户输入密码:”)
而NewPasswordReach==“”:#确保有输入
NewPasswordReach=原始输入(“请确认您帐户的密码:”)
如果newpassword==newpassword:#检查以确保密码正确
对于帐户中的用户名:#循环访问帐户中的所有用户名
if username.lower()==newusername.lower():#检查用户名是否已存在
打印“\n用户名已存在”
打印“请重试”
新帐户()
否则:#如果未使用用户名且密码正确,则会创建帐户
通过
UUID+=1#使当前UUID数增大1
accounts[newusername]={“password”:newpassword,“UUID”:UUID}创建一个新帐户
打印“\n已创建帐户”
打印“\n-------------------------------------------”
startup()#返回启动菜单
否则:#如果密码不匹配
打印“\n密码不匹配”
打印“请重试”
新帐户()
其他:
新帐户()
def login():
username=“”
password=“”
打印“\n-------------------------------------------”
打印“\n您想登录吗?”
yesorno=raw_input(“\n请输入YES或NO:”)#检查用户是否要登录
如果yesorno.lower()=“否”:
打印“\n-------------------------------------------”
启动()
elif yesorno.lower()=“是”:
对于帐户中的用户名2:#测试目的
打印用户名2,用于测试目的
打印“”#测试目的
对于帐户中的用户名23:#测试目的
对于str中的usernames3(账户[usernames23]):#测试目的
打印用户名3,用于测试目的
当username==“”时:#确保有输入
用户名=原始输入(“\n请输入您的用户名:”)
密码==“”:#确保有输入
密码=原始输入(“\n请输入密码:”)
对于帐户中的用户名:#遍历帐户中的所有用户名
if username.lower()==usernames.lower():#检查用户名输入是否等于帐户字典中的用户名
accountpassword=帐户['username'][密码]
accountUUID=0
如果密码==帐户密码:
对于帐户中的accountname:
accountpassword=帐户[帐户名称]
打印“\n已授予访问权限”
打印“\n-------------------------------------------”
菜单()
其他:
通过
打印“\n访问被拒绝”
打印“\n请
accountpassword = accounts[usernames]['password']
accountpassword = accounts[usernames]['UUID']
accounts[newusername] = {"password":accountpassword,"UUID":accountUUID}
for username in accounts:
if username == loginusername:
for account in accounts[username]
accountpassword = account['password']
accountuuid = account['uuid']