Skip to content
Advertisement

How can I write with a translator’s dictionary?

This is how my program is structured:

There is an n in the first line of the input that indicates the number of words in the translation dictionary. Each of the next n lines contains four words, the second to fourth words being the translation of the first word. Each word is translated into three different languages. The second word is the English translation, the third word is the French translation, and the fourth word is the German translation of the first word. The last line contains a sentence that needs to be translated from one of the English, French or German languages ​​into the first word. A sentence consists of several words separated by a space.

input:

4
man I je ich

kheili very très sehr

alaghemand interested intéressé interessiert 

barnamenevisi programming laprogrammation Programmierung

I am very interested in programming

Correct output:

man am kheili alaghemand in barnamenevisi

my code:

tedad = int(input())
d = dict(input().split(' ') for i in range(tedad))
c = ''
car = input().split(' ')

for x in car:
    if x in d:
        c+= ' '+d[x]

    else:
        c+=' '+x


print(c.strip())

My code has a problem and shows a wrong output. Please help me to correct the code and display it according to the sample output.

Advertisement

Answer

dict(input().split(' ') for i in range(tedad)) is not compatible with the inputs you give with not 2 words per line

One way can be from your code :

tedad = int(input())
d = {}
for i in range(tedad):
    l = input().split()
    for x in l[1:]:
        d[x] = l[0]

c = ''
car = input().split()

for x in car:
    c += ' ' + (d.get(x) or x)

print(c[1:])

having that in the file p.py and the input in i :

pi@raspberrypi:/tmp $ cat i
4
man I je ich
kheili very très sehr
alaghemand interested intéressé interessiert 
barnamenevisi programming laprogrammation Programmierung
I am very interested in programming
pi@raspberrypi:/tmp $ python3 p.py < i
man am kheili alaghemand in barnamenevisi
pi@raspberrypi:/tmp $ 
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement