字典/抽认卡问题。从值而不是键开始

2024-05-13 22:21:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想尝试这个抽认卡的想法,尝试学习关键字和他们的意义,为即将到来的测试。我想创建一个关于python的字典,我可以用它来帮助解决这个问题。我的想法是把定义展示给我,然后我必须猜出已经定义的单词。我在下面演示了如何先显示键,然后显示值,但是我希望它做相反的事情,但是我不知道如何实现它。(下面代码中的词汇表是指我的字典) 任何帮助都将不胜感激。你知道吗

def show_flashcard():
    """ Show a random key and ask me
        to define it. Show the definition
        when the user presses return.    
    """
    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])

Tags: thetokeyreturn字典定义showrandom
2条回答

您可以尝试:

def show_flashcard():
    """ Show a random definition and ask me
        the key. Show the key
        when the user presses return.    
    """
    random_key, random_def = choice(list(glossary.items()))
    print('Define: ', random_def)
    input('Press return to see the key')
    print(random_key)

如果我没听错的话,这还不够吗?你知道吗

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

基本上只是把问题和答案转换一下。你知道吗

相关问题 更多 >