简体   繁体   中英

randomly accessing dictionary values in python

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM