Change word in frasede according to dictionary

0

You have an exercise consisting of typing an N number and then typing a word and a quality into N lines. After this I must enter a sentence. If in this sentence there is a word that I had typed before, you should print their respective quality. Example

ENTRY

3
boldo explosiva
tampa chorosa
beijo calorosa
Vocˆe pˆos a tampa no boldo ?

Output

chorosa explosiva

My code is this one but it does not work.

dic={}
lista=[]

n=int(input()) 
for i in range(n):
    dic["palavra"],dic["adjetivo"]=input().split()

    lista.append(dic.copy())
frase=input()

for i in frase:
    if i in dic["palavra"]:
        print(dic["adjetivo"])
    
asked by anonymous 29.11.2018 / 02:15

1 answer

2

I think this is what you want, at least it gives the determined result:

dic = {}
n = int(input()) 
for i in range(n):
    palavra, adjetivo = input().split()
    dic[palavra] = adjetivo
frase = input().split()
for palavra in frase:
    if palavra in dic:
        print(dic[palavra])

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

What you are creating is a natural dictionary, it is not to create two of them, one column is the dictionary key and the other column is the value, this simplifies the code because it does not even need any list. The selection of the phrase does not make much sense. I had check if the word is available throughout the dictionary, your code only looked at an item (actually the only one) and then you get the value of the item according to the key (the word being checked) that you already knows it exists. I changed the variable name to something more meaningful. And I made a split() in the sentence too, to separate the words, I was not doing this.

    
29.11.2018 / 03:05