繁体   English   中英

更新字典时,Nonetype是不可迭代的吗?

[英]Nonetype is not iterable when updating dictionary?

我对python很陌生,作为一个推动自我的项目,我决定创建一个具有可更新的user:password字典的简单框架。 这项工作仍在进行中! 创建新的用户ID,然后尝试访问该用户ID时存在问题。 我正在使用python 3.7。

这是错误:追溯(最近一次呼叫最近):行37,如果在帐户中使用帐户:TypeError:类型'NoneType'的参数不可迭代

import sys
accounts = {'Trace': 'Jollyrancher5', 'Brian': 'Kitties82', 'Taylor': 'Flower15'}

while True:
    print('Please select an option.\n1. Create new account.\n2. Enter existing account.')
    choice = input()
    if choice == '1':
        print('Please enter an account name')
        new_account = input()
        if new_account not in accounts.keys():
            print('Please enter a password.')
            new_pass = input()
            accounts = accounts.update({new_account: new_pass})
            print('Your new User ID is: ' + new_account + '.')
            print('Your new password is: ' + new_pass + '.')
            print('Please store this information for safe keeping.')
            print('Type OK to continue.')
            while True:
                next = input()
                if next == 'OK' or next == 'ok':
                    break
                else:
                    print('Invalid entry. Please type OK.')
        else:
            print('Account name taken. Please enter a different account name.')
    elif choice == '2':
        break
    else:
        print('Not a valid entry.')

account = ''
password = ''
denial = 0
while True:
    print('Please enter User ID.')
    account = input()
    if account in accounts:
        break
    else:
        print('User ID not recognized.')

while True:
    print('Please enter password.')
    password = input()
    if password == accounts[account]:
        break
    else:
        password != accounts[account]
        denial += 1
        if denial == 3:
            print('Account locked.')
            input()
            sys.exit()
print('Access Granted.\nYour account balance is $1,000,000.00.')
input()

问题在第13行:

accounts = accounts.update({new_account: new_pass})

由于更新已完成,因此将返回“ None ”。 [ 请参阅docs ]而是使用:

accounts.update({new_account: new_pass})

它将执行更新,而无需重新分配值。

删除此行:-

`accounts = accounts.update({new_account: new_pass})`

添加这一行:

`while True:
      print('Please enter User ID.')
      account = input()
      accounts.update({new_account:new_pass})
      if account in accounts:`

导致此错误的原因是您的字典没有更新,这就是为什么它收到的第一个值是None,所以生成了NonType错误。

如果您理解我的回答,请支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM