所以,我有一个想法,我可以使用一系列数字作为字典中单个值的键。作为Python中的字典键的范围
我写了下面的代码,但我不能得到它的工作。它甚至有可能吗?
stealth_roll = randint(1, 20)
# select from a dictionary of 4 responses using one of four ranges.
## not working.
stealth_check = {
range(1, 6) : 'You are about as stealthy as thunderstorm.',
range(6, 11) : 'You tip-toe through the crowd of walkers, while loudly calling them names.',
range(11, 16) : 'You are quiet, and deliberate, but still you smell.',
range(16, 20) : 'You move like a ninja, but attracting a handful of walkers was inevitable.'
}
print stealth_check[stealth_roll]
而不是试图使用范围的关键为什么不根据字典的大小滚动? – TheLazyScripter
作为一个旁注,这样的字典是可能的python3。由于键是范围,因此您必须相应地访问该词典:'stealth_check [range(6,11)]'将起作用。这对你的目的来说完全没用,只是想显示对象模型是一致的。 – spectras
@TheLazyScripter我在整个脚本中采用了(1,20)惯例,我通常也使用随机值作为乘数以及场景选择器。另外,如果这个工作,我可以对每个可能的结果应用不同的权重。 –