gpt4 book ai didi

python-3.x - 在随机选择键后从字典中随机选择一个值

转载 作者:行者123 更新时间:2023-12-05 05:09:11 27 4
gpt4 key购买 nike

我正在尝试用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com