A vocabulary trainer

Peter Otten __peter__ at web.de
Mon Aug 28 16:59:18 EDT 2017


Stefan Ram wrote:

>   My course participants always are impatient for "useful
>   applications". So at a point in my course where no control
>   structures (if, for, while, ...) have been introduced yet,
>   but function calls, function declarations, assignments,
>   lists and dictionaries already were introduced, I wanted to
>   show a vocabulary trainer.
> 
>   One starts it by »main()«. It then asks for a translation
>   of a word to German:
> 
>>>> main()
> table
> 
>   . Now, one can think about the answer and eventually press
>   return to see the answer:
> 
> Tisch
> horse
> 
>   and at the same time, the next question is shown.
> 
>   Here is the source code:
> 
> vocs = { 'table': 'Tisch', 'book': 'Buch', 'rain': 'Regen', 'horse':
> 'Pferd' }

For this use case I would prefer a list of tuples

word_pairs = [
    ("table", "Tisch"),
    ...
]

Then

def main():
    while True:
        en, de = random.choice(word_pairs)
        input(en)
        print(de)

> import random
> 
> def main():
>     v = random.choice( list( vocs.keys() ))
>     print( v, end='' )
>     input()
>     print( vocs[ v ]);
>     main()
> 
>   Are there improvements possible (like shorter source code
>   or a better programming style)? (The recursion will be
>   replaced by »while« as soon as »while« is introduced.)
> 
>   On the console, I used:
> 
> i = input()
> 
>   just to hide the result of »input()«. I only write to »i«,
>   I do not read from it. JavaScript has »void« to convert
>   something to »undefined«. Is there a standard means in
>   Python to convert a value to »None« (which also would have
>   the effect of not showing the value)?
> 





More information about the Python-list mailing list