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

时间:2015-10-21 21:54:24

标签: python

我已将代码更改为:

body

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

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'

1 个答案:

答案 0 :(得分:2)

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'