【问题标题】:Flashcard toss a coin dictionary抽认卡抛硬币词典
【发布时间】:2023-01-04 03:34:34
【问题描述】:

我正在尝试从字典中以随机顺序打印键或值。 (随机是先显示词条还是先显示相应的定义。)

但我只得到一个键,然后是一个值。 我缺少什么代码才能工作?

例子:

  • Test-1(按回车键)Definition-1
  • Definition-4(按回车键)Test-4
  • Definition-2(按回车键)Test-2
  • 测试 3(按回车键)定义 3 ...
 from random import *
    
    def flashcard():
        random_key = choice(list(dictionary))
        print('Define: ', random_key)
        input('Press return to see the definition')
        print(dictionary[random_key])
    
    dictionary = {'Test-1':'Definition-1',
                'Test-2':'Definition-2',
                'Test-3':'Definition-3',
                'Test-4':'Definition-4'}
    
    exit = False while not exit:
        user_input = input('Enter s to show a flashcard and q to quit: ')
        if user_input == 'q':
            exit = True
        elif user_input == 's':
            flashcard()
        else:
            print('You need to enter either q or s.')

【问题讨论】:

  • 您的示例是您希望输出的样子还是现在的样子?
  • 我想要的样子,我试过随机选择,我试过交换它但无法让它工作:/

标签: python python-3.x dictionary random


【解决方案1】:

您可以在 0 和 1 之间选择一个随机整数,也可以在 0 和 1 之间选择。

def flashcard():
    k, v = choice(list(dictionary.items()))
    # toss a coin to decide whether to show the key or the value
    # alternative is randint(0, 1)
    if choice((True, False)):
        print('Define: ', k)
        input('Press return to see the definition')
        print(v)
    else:
        print('What is: ', v)
        input('Press return to see the key')
        print(k)

【讨论】:

  • 我喜欢这个,易于理解并让他们可以选择使用值检索密钥
【解决方案2】:

使用random.random 确定是否应首先显示 random_key 或定义

from random import *


def flashcard():
    random_key = choice(list(dictionary))
    if random() > 0.5:
        print("Define: ", random_key)
        input("Press return to see the definition")
        print(dictionary[random_key])
    else:
        print("Define: ", dictionary[random_key])
        input("Press return to see the key")
        print(random_key)


dictionary = {
    "Test-1": "Definition-1",
    "Test-2": "Definition-2",
    "Test-3": "Definition-3",
    "Test-4": "Definition-4",
}

exit = False
while not exit:
    user_input = input("Enter s to show a flashcard and q to quit: ")
    if user_input == "q":
        exit = True
    elif user_input == "s":
        flashcard()
    else:
        print("You need to enter either q or s.")

【讨论】:

    【解决方案3】:

    另一种选择是使用sample(学习more):

    from random import choice,sample
    
    def flashcard():
        print('Define: {}
    The key: {}'.format(*sample(choice([*dictionary.items()]),2)))
    
    dictionary = {'Test-1':'Definition-1',
                'Test-2':'Definition-2',
                'Test-3':'Definition-3',
                'Test-4':'Definition-4'}
    
    # testing
    for _ in range(3):
        flashcard()
        print()
    
    >>> out
    '''
    Define: Test-4
    The key: Definition-4
    
    Define: Definition-1
    The key: Test-1
    
    Define: Definition-2
    The key: Test-2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2012-12-02
      • 2017-12-04
      • 2012-06-24
      • 1970-01-01
      相关资源
      最近更新 更多