randomly accessing dictionary values in python

67 Views Asked by At

So I have code, where it should choose at random whether to show the user the entry itself or the associated definition.

from random import *


def show_flashcard():

    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

It only shows the random keys, but not the values. How can i implement this in my code ?

1

There are 1 best solutions below

1
Happy Ahmad On

With choice(list(glossary.values())), you can get a random value from your dict.