我正在尝试用 python 制作一个命运之轮类型的游戏。我有从字典中随机选择一个键的代码,但我不确定如何从该特定键中选择一个随机值。
categories = {'Sayings': ['Actions speak louder than words', 'A bird in the hand is worth two in the bush',
'All good things come to an end', 'Among the blind the one eyed man is king',
'Fortune favors the bold', 'Ignorance is bliss'],'70s Soul Musicians': ['James Brown', 'Sly Stone', 'Aretha Franklin', 'Earth Wind & Fire', 'Stevie Wonder',
'Chaka Khan']}
程序选择一个随机键/类别让玩家猜测。
randCategory = str(random.choice(list(categories)))
print(randCategory)
最佳答案
试试这个
import random
categories = {
'Sayings': [
'Actions speak louder than words',
'A bird in the hand is worth two in the bush',
'All good things come to an end',
'Among the blind the one eyed man is king',
'Fortune favors the bold',
'Ignorance is bliss'
],
'70s Soul Musicians': [
'James Brown',
'Sly Stone',
'Aretha Franklin',
'Earth Wind & Fire',
'Stevie Wonder','Chaka Khan'
]
}
randCategory = str(random.choice(list(categories)))
randValue = str(random.choice(list(categories[randCategory])))
print(randCategory)
print(randValue)
关于python-3.x - 在随机选择键后从字典中随机选择一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57748633/