2014-09-28 80 views
0

我想知道是否可以将字典项目保存到变量。所以,这就是我正在做的。我保存此项目的字典:将dictionarys项目设置为变量

accounts{} 

def accountcreator(): 
    newusername = raw_input() 
    newpassword = raw_input() 
    UUID = 0 
    UUID += 1 
    accounts[newusername] = {newpassword:UUID} 

现在基本上我将通过newusernames在一个单独的函数来循环:

def accounts(): 
    username = raw_input() 
    for usernames in accounts: 
    if usernames == username: 
     #Not sure what to do from here on out 
    else: 
     accounts() 

这是我感到困惑。因此,如果用户名输入在帐户字典中等于新用户名,它将继续保留。我希望它将newusernames密码和UUID({newpassword:UUID}部分)保存到变量中。所以基本上,如果newusername等于用户名输入,它会将其余信息({newpassword:UUID})保存到变量中。所以最后变量可以说accountinfo = {newpassword:UUID}。谢谢,我希望这是有道理的。

+0

您只需执行'accountinfo = accounts [username]'。阅读一些python教程。 – user3885927 2014-09-28 15:34:07

+0

那我怎么也抓住UUID?或者,如果这是UUID,我怎样才能获取密码?谢谢 – 2014-09-28 15:45:40

回答

1

代码中有几个错误。首先,可能是一个错字:

accounts = {} 

接下来,当你创建代码,你总是重置UUID为0,使得增量一点毫无意义。初始化UUID功能外,像你这样做accounts

UUID = 0 
def accountcreator(): 
    newusername = raw_input() 
    newpassword = raw_input() 
    UUID += 1 
    accounts[newusername] = {newpassword:UUID} 

第三,我不知道为什么要映射的密码到UUID。可能的是,你要在用户字典两个单独的领域来存储:

accounts[newusername] = { 'password': newpassword, 'UUID': UUID } 

最后,使用字典将用户名映射到信息是,你并不需要遍历整个字典的整点;您只需使用用户名索引字典。不过,您必须注意不要尝试访问不存在的密钥。

# Use a different name; accounts is already a dictionary 
def get_account(): 
    username = raw_input() 
    if username in accounts: 
     return accounts[username] 
    else: 
     print("No account for {0}".format(username)) 
+0

你好,我做了你所说的:accounts [newusername] = {'password':newpassword,'UUID':UUID}。现在,我将如何获取密码和UUID并将它们保存到vaiables?这是我迄今为止:用于帐户的用户名*新行*密码=帐户[用户名] – 2014-09-29 01:07:44

+0

请帮助^^ ^^ ^^ – 2014-09-29 22:34:02