我通过一个程序发送这段代码,以决定用户将要对抗哪些怪物,但每次进行此操作都会产生“Headless_Horseman(或者它选择的任何一个怪物)未定义”。我无法弄清楚如何'定义'这个名字。
def mob_picker_randmob1 ():
global Zombie
global Zambie
global Giant_Worm
global Headless_Horseman
global Skeleton
global Ghost
global Ghoul
global Baby_Sister
global Little_Sister
global Big_Sister
randmob1=random.randint(1,5)
if randmob1 == 1:
randmob1 = Zombie
mob_picker_randmob2 ()
elif randmob1 == 2:
randmob1 = Skeleton
mob_picker_randmob2 ()
elif randmob1 == 3:
randmob1 = Giant_Worm
mob_picker_randmob2 ()
elif randmob1 == 4:
randmob1 = Headless_Horseman
mob_picker_randmob2 ()
elif randmob1 == 5:
randmob1 = Zambie
mob_picker_randmob2 ()
答案 0 :(得分:0)
您需要为主函数中的变量赋值,因为您将这些值声明为全局变量。
Zombie = 'zombie'
Zambie = 'zAmbie'
Giant_Worm = 'big worm'
Headless_Horseman = 'horseman without head'
另外,请考虑使用random.choice()
choices = (
Zombie, Zambie, Giant_Worm, Headless_Horseman, Skeleton,
Ghost, Ghoul, Baby_Sister, Little_Sister, Big_Sister,
)
return random.choice(choices)