Python 搁置不起作用

Python 搁置不起作用,python,python-3.x,Python,Python 3.x,我正在编写一个简单的程序,为您提供库中的单词。用户也可以编辑该库。出于某种原因,即使这个词在图书馆里,也无法搜索(至少我认为是这样)。下面的第一段代码是整个程序。我将在底部解释哪个块出错 print("WordCenter") import shelve,pickle,sys #Create the main program def do(): f = shelve.open("Data.dat") quitq = input("x>Serch z>Quit a>

我正在编写一个简单的程序,为您提供库中的单词。用户也可以编辑该库。出于某种原因,即使这个词在图书馆里,也无法搜索(至少我认为是这样)。下面的第一段代码是整个程序。我将在底部解释哪个块出错

print("WordCenter")
import shelve,pickle,sys
#Create the main program
def do():
    f = shelve.open("Data.dat")
    quitq = input("x>Serch z>Quit a>Add")
    if quitq == "z":
        f.close
        sys.exit()
    elif quitq == "x":
        key = input("Key:")
        try:
            word = f[key]
            print("The word is:",word)


        except:
            print("The word is not there")
        do()
    elif quitq == "a":


        try:
            x = f["curo"]
            y = f["curt"]
            z = f["curtr"]
            x = int(x)
            y = int(y)
            z = int(z)
            x,y,z = (x+1,y+1,z+1)
            f["curo"] = x
            f["curt"] = y
            f["curtr"] = z
        except:
            f["curo"] = "1"
            f["curt"] = "1"
            f["curtr"] = "1"
            x,y,z = 1,1,1

        word = input("Word:")
        key = x+y+z
        key = str(key)
        print("Key for",word,"is",key,".")
        f[key] = word
        f.sync()
        do()
#Initlize the app
do()
这一行就是它出错的地方:
word=f[key]
。下面是出错的代码块

elif quitq == "x":
        key = input("Key:")
        try:
            word = f[key]
            print("The word is:",word)


        except:
            print("The word is not there")
下面的一小段是生成访问定义的密钥的部分

elif quitq == "a":


        try:
            x = f["curo"]
            y = f["curt"]
            z = f["curtr"]
            x = int(x)
            y = int(y)
            z = int(z)
            x,y,z = (x+1,y+1,z+1)
            f["curo"] = x
            f["curt"] = y
            f["curtr"] = z
        except:
            f["curo"] = "1"
            f["curt"] = "1"
            f["curtr"] = "1"
            x,y,z = 1,1,1

        word = input("Word:")
        key = x+y+z
        key = str(key)
        print("Key for",word,"is",key,".")
        f[key] = word
        f.sync()
最后,下面是错误消息。我必须撤消
尝试:
例外:
才能让它显示出来

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 111, in __getitem__
    value = self.cache[key]
KeyError: '6'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/liujack/Documents/QQR.py", line 46, in <module>
    do()
  File "/Users/liujack/Documents/QQR.py", line 12, in do
    word = f[key]
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 113, in __getitem__
    f = BytesIO(self.dict[key.encode(self.keyencoding)])
KeyError: b'6'
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py”,第111行,在__
value=self.cache[key]
键错误:“6”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Users/liujack/Documents/QQR.py”,第46行,在
do()
文件“/Users/liujack/Documents/QQR.py”,do中的第12行
word=f[key]
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py”,第113行,在__
f=字节(self.dict[key.encode(self.keycodencing)])
键错误:b'6'
下面的字符在.dat文件中。(我用TextEdit打开了它)

a“(ïn)¸ÒÏ·”ÄX1q.curtrÄX1q.curtÒFL-
FLFÄXAdamantq.3ÄX1q.curo˝7171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171717171

如果可以,请回复!!
谢谢

@anon是的,现在这个问题已经解决了!为什么您仍然使用f=shelve.open而不是shelve.open(文件)的
as f:
还有,你为什么要进口泡菜?我看不出有人在使用它…@DeliriousSyntax抱歉,这只是我的习惯,进口泡菜和货架放在一起…只是因为它们关系密切!我看到人们一直在这样做,却不知道他们为什么这么做。