我在生成随机对象时遇到问题(仍然),在这种情况下是通过觅食发现的随机草药。这是该函数的代码:
def collectPlants(self):
if self.state == 'normal':
print"%s spends an hour looking for medicinal plants." % self.name
if random.choice([0,1]):
foundHerb = random.choice(herb_dict)
print "You find some %s." % foundHerb
return random.choice(herb_dict)
else: print"%s doesn't find anything useful." % self.name
和 dict 块:
herb_dict = [
("Aloe Vera", Player().health == Player().health + 2),
("Cannabis", Player().state == 'high'),
("Ergot", Player().state == 'tripping')
]
对不起,秘密的例子。Herb 也是一个具有三个参数的类:(self, name, effect)。
调用 collectPlants 函数时,如何从字典中生成随机草本植物?