我正在进行基于文本的冒险,现在想运行一个随机函数。所有冒险功能都是“adv”,后跟一个 3 位数字。
如果我运行 go() 我会回来:
IndexError: Cannot choose from an empty sequence
这是因为 allAdv 仍然是空的。如果我在 shell 中逐行运行 go() 它可以工作,但不能在函数中运行。我错过了什么?
import fight
import char
import zoo
import random
#runs a random adventure
def go():
allAdv=[]
for e in list(locals().keys()):
if e[:3]=="adv":
allAdv.append(e)
print(allAdv)
locals()[random.choice(allAdv)]()
#rat attacks out of the sewer
def adv001():
print("All of a sudden an angry rat jumps out of the sewer right beneath your feet. The small, stinky animal aggressivly flashes his teeth.")
fight.report(zoo.rat)