【发布时间】: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