A random word from one of two lists

Bischoop Bischoop at vimart.net
Sat Jan 2 13:36:47 EST 2021


On 2021-01-02, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>
>   The following might /not/ be good programming style,
>   but addresses the idea to choose a "variable".
>
I kinda like the below one.
> import random
> animal = ['koala', 'kangaroo']
> fruit = ['banana', 'apple']
> kinds = ['animal','fruit']
> variable = random.choice( kinds )
> result = random.choice( globals()[ variable ])
> print( result )
>


>   Otherweise, I'd go this way without a dictionary.
> import random
> animal = ['koala', 'kangaroo']
> fruit = ['banana', 'apple']
> kinds = [animal,fruit]
> kind = random.choice( kinds )
> result = random.choice( kind )
> print( result )
>
I had that solution in mind but I thought that one is not good
programming style or not Pythonin :-)


--
Thanks
>


More information about the Python-list mailing list