这里有点初学者。如果选择了列表中的特定项目,我希望我的代码返回特定字符串。
import random
# carrots
# yarn
list = ['bunny', 'kitty']
random.choice (list)
因此,如果'bunny'
选择say ,那么我希望它发布:
兔子 - 胡萝卜
但我不希望“兔子”与“胡萝卜”以外的任何东西搭配,所以没有“纱线”。我该怎么做?
将项目对放在字典中,然后使用其中一个键选择一个项目:
import random
# pair the items in a dictionary
items = {'bunny': 'carrots', 'kitty': 'yarn'}
# get a list of the keys in the dictionary
keys = list(items.keys())
# select a key
key = random.choice(keys)
# print the key and what it is paired with
print(key + ' - ' + items[key])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句