gpt4 book ai didi

python - 是什么导致 'int' 对象没有属性 'has_key' ?

转载 作者:行者123 更新时间:2023-12-01 04:24:54 24 4
gpt4 key购买 nike

我已将代码更改为:

print '\n'
search = raw_input("For which account are you searching: ")
f = shelve.open("passwords.dat")
for line in f:
if search in f:
passwrd = f[search]
entry = passwrd[0]
f.close()
print line
print '\n'
print "I'm sorry we could not find any account related to " + search
print '\n'
f.close()

现在它不会接受任何正确的帐户并发出此错误:

'int' object has no attribute 'has_key'

为什么我现在收到此消息?

  File "E:\password.py", line 98, in program_start
find_account()
File "E:\password.py", line 36, in find_account
if search in f:
File "C:\Python24\lib\shelve.py", line 107, in __contains__
return self.dict.has_key(key)
AttributeError: 'int' object has no attribute 'has_key'

最佳答案

Shelve 返回单个字典。
因此,据我所知,从中读取多行是没有意义的。

我认为这应该有效:

print '\n'
search = raw_input("For which account are you searching: ")
with shelve.open("passwords.dat") as f:
for passwords in f:
if search in passwords:
password = passwords[int(search)]
entry = passwrd[0]
print passwords
print '\n'
else:
print "I'm sorry we could not find any account related to " + search
print '\n'

关于python - 是什么导致 'int' 对象没有属性 'has_key' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com