在這個程序中,我正在製作一本字典。我做了詞典,當我打字時,它說詞典不存在於詞典中,爲什麼?
當我運行這個程序,並在程序中要求我search meaning
菜單1
,但是當我鍵入單詞404
(這是在字典中),它說:Word donot exist in dictionary
。這個問題從哪裏來?
print("This is a dictioinary")
print("\t\t\tWelcome to Geek Translator Game")
dictionary={"404":"Message not found","googler":"person who seaches on google"}
choose=None
while(choose!=0):
choose=input('''0.Exit
1.search meaning
2.Add Word
3.Replace meaning''')
if choose is 0:
print("bye")
if choose is 1:
word=input("Enter the word\n")
if word in dictionary:
meaning=dictionary[word]
print(meaning)
else:
print("Word donot exist in dictionary")
if choose is 2:
word=input("Enter the word\n")
if word in dictionary:
print("Word already exists in dictionary")
print("Try replacing meaning by going in option 3")
else:
defination=input("Enter the defination")
dictionary[word]=defination
if choose is 3:
word=input("Enter the term\n")
if word in dictinary:
meaning=input("Enter the meaning\n")
dictionary[word]=meaning
else:
print("This term donot exist in dictionary")
'input'在Python 2.x的是真的'的eval(的raw_input(...))',所以'word'是多少' 404'(其中*不是字典中的*)而不是字符串''404'(是)。 **使用'raw_input'。** – jonrsharpe 2014-12-05 10:51:58
另外,您應該使用'=='而不是'is'(您正在測試的是平等,而不是身份)。 – 2014-12-05 10:53:00