Create an English dictionary with Python for in syntax

Asked 2 years ago, Updated 2 years ago, 13 views

I am making an English dictionary using for in syntax.

 dic = {
    'apple' : 'Apple',
    'banana' : 'banana',
    'orange' : 'orange',
    'music' : 'music',
    'door' : 'door',
    "Bear" : "Bear",
    'dog' : 'dog',
    'cat' : 'Cat'
}


for i in range(len(dic)):
word = input ('word input:')
if word in dic:
    print('meaning: {}'.format(dic[word]))
else:
    print('This word is not in the dictionary. Please register the meaning.')

I've done it up to here

You have to add words that don't exist and save them in the dic ㅜ<

What do I do?

python

2022-09-20 17:10

2 Answers

I added a stop function in the middle, but it worked anyway.

dic = {
    'apple' : 'Apple',
    'banana' : 'banana',
    'orange' : 'orange',
    'music' : 'music',
    'door' : 'door',
    "Bear" : "Bear",
    'dog' : 'dog',
    'cat' : 'Cat'
}


for i in range(len(dic)):
    word = input ('word input:')
    if word in dic:
        print('meaning: {}'.format(dic[word]))
    elif word == 'stop':
        break
    else:
        print('This word is not in the dictionary. Please register the meaning.')
        mean = input('Enter meaning :')
        dic[word] = mean


2022-09-20 17:10

When adding dict elements, you can give an update command.

dic = {
    'apple' : 'Apple',
    'banana' : 'banana',
    'orange' : 'orange',
    'music' : 'music',
    'door' : 'door',
    "Bear" : "Bear",
    'dog' : 'dog',
    'cat' : 'Cat'
}


Please enter word = input(').\nWord : ')
if word in [i for i in dic]:
    print('meaning: {}'.format(dic[word]))

# # [i for i in dic] >> ['apple', 'banana', 'orange', 'music', 'door', 'bear', 'dog', 'cat']

else:
    print('This word is not in the dictionary. Please register the meaning.')
    Please enter the meaning of the word mean = input(').\nMeaning :')
    newword = {word:mean}
    dic.update(newword)

print(dic)


2022-09-20 17:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.