当前位置: 代码迷 >> python >> 是什么导致“ int”对象没有属性“ has_key”?
  详细解决方案

是什么导致“ int”对象没有属性“ has_key”?

热度:47   发布时间:2023-06-13 15:08:59.0

我已将代码更改为:

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'

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

我认为这应该工作:

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'