Shubham Sharma
Shubham Sharma

Reputation: 1831

i made dictionary, when i am typing word it says the word donot exist in dictionary,why?

In this program i am making a dictionary.

When i run this program and enter 1 in the menu the program asks me to search meaning, but when i type the word 404(which is in the dictionary) it says Word donot exist in dictionary. Where does this problem come from?

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")

Upvotes: 0

Views: 68

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123830

input() interprets user input as a Python expression. If you enter 404, Python interprets that as an integer. Your dictionary, however, contains a string.

You'll have to enter "404" with quotes for this to work correctly. Your better option would be to use raw_input() instead, to get the raw input the user typed without it having to be formatted as a Python expression:

word = raw_input("Enter the word\n")

Do this everywhere you use input() now. For your user menu input, you should really use int(raw_input("""menu text""")) rather than input(). You might be interested in Asking the user for input until they give a valid response to learn more about how to ask a user for specific input.

Next, you are using is to test user choices. That this works at all is a coincidence, as Python has interned small integers you are indeed getting the same 0 object over and over again and the is test works. For almost all comparisons you want to use == instead however:

if choose == 0:
    # ...
elif choose == 1:
    # etc.

Upvotes: 3

Related Questions