A vocabulary trainer

Chris Angelico rosuav at gmail.com
Mon Aug 28 16:14:18 EDT 2017


On Tue, Aug 29, 2017 at 5:42 AM, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> def main():
>     v = random.choice( list( vocs.keys() ))

Should be able to just use list(vocs) here.

>     print( v, end='' )
>     input()

input(v) should do the same job of issuing a prompt.

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

Strongly recommend using a 'while' loop rather than recursion, even
from the start. Don't teach bad practices and then replace them later.
If you have to, do something like:

while True: # don't worry about what this line does yet

and then explain it later. Better to have a bit of magic that you
later explain than bad code that people will copy and paste.

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

That's only an issue when you're working interactively, so I wouldn't
worry about it. Either assign to a junk variable ("_ = whatever" is
common) or just let it get printed.

ChrisA



More information about the Python-list mailing list