Hi. I'm new to python, and also have a poor english so...excuse me!:icon_question:

Ok, I want to generate a dictionary like this one:

di={'word1':['tra1','A brief definition.'],'word2':['tra2','Another definition.']}

from a txt file containing the word, its translation (tra#) and a definition of the word:

word1 trad1 'A brief definition.'
word2 trad2 'Another definition.'

Really pleased if you can help me suggesting another structure for the dictionary, or using other format for the txt or any kind of help :)

5 Years
Discussion Span
Last Post by pyTony

It is unlikely that the words have only one translation so you are likely to need to deepen the definition value to list of lists or tuple. Also translation part is likely to need quoting or alternatively I find it convenient to use semicolon as separator in text file so that I can save parts without quoting.

Edited by pyTony: n/a


Thanks tony, I forgot to mention that I need only one of the possible translation.

A bit of context: the game is for kids learning english here at my town, of course that game is still only an idea: The definition will be displayed (in spanish), so you have to guess the word in english (the keys) and I need the translated word to be displayed after a word is guessed.

Edited by dobleseis: n/a


You have then predetermined the three elements needed, you print 3rd (definition), and want to check input against second, looks like the spannish translation is not needed. For me your program looks imply random.choice() from list of list of word definitions.
If you want to ask say 10 different words it is best to take sample of 10 by random.sample().

words_finnish = (('This animal says hau-hau', 'koira', 'dog'), ('It is cold white food eaten in summer time', 'jäätelö', 'ice cream'))
def check(word, guess):
    return ('Yes, %r is %r in Finnish!' % ( guess, word[-1])) if guess.strip().lower() == word[1] else ('No, %r is not correct answer!' % guess)
>>> import random
>>> sana = random.choice(words_finnish)
>>> sana[0]
'This animal says hau-hau'
>>> check(sana, 'kissa')
"No, 'kissa' is not correct answer!"
>>> check(sana, 'koira')
"Yes, 'koira' is 'dog' in Finnish!"

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.