我对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()
答案 0 :(得分:1)
删除此行:-
`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错误。
如果您理解我的回答,请投票。