Change word in jars according to dictionary

Asked

Viewed 329 times

0

There is an exercise that consists of typing an N number and then typing in N lines a word and a quality. After this I must type a sentence. If in this sentence there is a word that I had typed before, its quality should be printed. Example

ENTREE

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

Exit

chorosa explosiva

My code is this one but it doesn’t 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"])
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.