搁置不工作

时间:2016-01-27 02:47:57

标签: 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>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()

最后,下面是错误消息。我必须撤消try:except:才能显示。

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'

以下字符位于.dat文件中。 (我用TextEdit打开它)

a“(ïn}„¸ÒÏ·”·ÄX1q.curtrÄX1q.curt¸Òfl— flÄXAdamantq.3ÄX1q.curo˝ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ

如果你能回复!! 感谢

0 个答案:

没有答案