为什么我在下面的代码中会在 Python3 中出现 KeyError?

Why am I getting KeyError in Python3 in the following piece of code?

    d = { ' dog ' : ' has a tail and goes woof! ' ,' cat ' : ' says  meow ' ,' mouse':' chased by cats ' }

    word=input('Enter a word: ')
    print('The definition is:', d.get(word))


    Traceback (most recent call last):
       File "<pyshell#429>", line 1, in <module>
        print('The definition is: ', d[word])
    KeyError: 'dog'
    

我输入了 dog 作为我的键值,希望它打印出来:'有一条尾巴并且发出汪声! ' 但我得到了一个 KeyError。我该如何解决?

字符串中有尾随空格和前导空格,例如'狗',那样你永远找不到钥匙。此代码应该可以正常工作:

d = { 'dog' : 'has a tail and goes woof!' ,'cat': 'says  meow', ' mouse':'chased by cats' }

word=input('Enter a word: ')
print('The definition is:', d.get(word.strip()))

我在末尾添加了 word.strip() 以忽略输入中的尾随和前导空格