全局变量未定义

时间:2010-07-30 16:40:28

标签: python random

我正在调用两个单独的函数来确定“P01”等于什么。第一个选择一个随机数,并丢弃已经选择的随机数。第二个采用随机数的结果并选择一个变量使“位置”相等。然后我说'P01'等于'位置'。

我让'position'成为一个全局变量,但是当我尝试将'position'分配给'P01'时说“位置未定义”时,我一直收到错误。

有什么想法吗?

### Monster Statistics ####

Cerebus = {'name': 'Cerebus','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Cthulhu = {'name': 'Cthulhu','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Cyclops = {'name': 'Cyclops','HP1': 65,'HP2': 85,'HP3': 95,'HP4': 130,'Atk1L1': 3,'Atk1L2': 4,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 3,'Atk1L2dmg': 4,'Atk1L3dmg': 4,'Atk1L4dmg': 5,'Atk2L1': 2,'Atk2L2': 3,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 5,'Atk2L2dmg': 6,'Atk2L3dmg': 6,'Atk2L4dmg': 7,'Atk3L1': 1,'Atk3L2': 2,'Atk3L3': 3,'Atk3L4': 3,'Atk3L1dmg': 5,'Atk3L2dmg': 6,'Atk3L3dmg': 6,'Atk3L4dmg': 7,'Dfns1': 2,'Dfns2': 3,'Dfns3': 4,'Dfns4': 5,}
Genie = {'name': 'Genie','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
GiantApe = {'name': 'Giant Ape','HP1': 70,'HP2': 90,'HP3': 110,'HP4': 140,'Atk1L1': 3,'Atk1L2': 4,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 4,'Atk1L2dmg': 5,'Atk1L3dmg': 5,'Atk1L4dmg': 6,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 5,'Atk2L2dmg': 6,'Atk2L3dmg': 6,'Atk2L4dmg': 7,'Atk3L1': 1,'Atk3L2': 2,'Atk3L3': 3,'Atk3L4': 3,'Atk3L1dmg': 6,'Atk3L2dmg': 6,'Atk3L3dmg': 7,'Atk3L4dmg': 8,'Dfns1': 2,'Dfns2': 3,'Dfns3': 3,'Dfns4': 4,}
GiantLizard = {'name': 'Giant Lizard','HP1': 80,'HP2': 100,'HP3': 130,'HP4': 170,'Atk1L1': 3,'Atk1L2': 3,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 3,'Atk1L2dmg': 4,'Atk1L3dmg': 5,'Atk1L4dmg': 6,'Atk2L1': 2,'Atk2L2': 3,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 4,'Atk2L2dmg': 5,'Atk2L3dmg': 6,'Atk2L4dmg': 7,'Atk3L1': 1,'Atk3L2': 2,'Atk3L3': 2,'Atk3L4': 3,'Atk3L1dmg': 6,'Atk3L2dmg': 7,'Atk3L3dmg': 8,'Atk3L4dmg': 8,'Dfns1': 1,'Dfns2': 2,'Dfns3': 2,'Dfns4': 3,}
GreyAlien = {'name': 'Grey Alien','HP1': 30,'HP2': 40,'HP3': 45,'HP4': 50,'Atk1L1': 3,'Atk1L2': 3,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 2,'Atk2L2dmg': 3,'Atk2L3dmg': 3,'Atk2L4dmg': 4,'Atk3L1': 1,'Atk3L2': 2,'Atk3L3': 3,'Atk3L4': 4,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 3,'Dfns2': 4,'Dfns3': 4,'Dfns4': 5,}
Gryffin = {'name': 'Gryffin','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Leprechaun = {'name': 'Leprechaun','HP1': 30,'HP2': 35,'HP3': 50,'HP4': 65,'Atk1L1': 3,'Atk1L2': 4,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 1,'Atk1L2dmg': 1,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 2,'Atk2L2': 3,'Atk2L3': 4,'Atk2L4': 5,'Atk2L1dmg': 2,'Atk2L2dmg': 2,'Atk2L3dmg': 3,'Atk2L4dmg': 4,'Atk3L1': 1,'Atk3L2': 2,'Atk3L3': 3,'Atk3L4': 4,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 6,'Atk3L4dmg': 6,'Dfns1': 4,'Dfns2': 6,'Dfns3': 7,'Dfns4': 8,}
Medusa = {'name': 'Medusa','HP1': 20,'HP2': 30,'HP3': 45,'HP4': 50,'Atk1L1': 3,'Atk1L2': 3,'Atk1L3': 4,'Atk1L4': 4,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 2,'Atk2L2dmg': 2,'Atk2L3dmg': 3,'Atk2L4dmg': 3,'Atk3L1': 1,'Atk3L2': 1,'Atk3L3': 2,'Atk3L4': 3,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 2,'Dfns2': 3,'Dfns3': 4,'Dfns4': 4,}
Minotaur = {'name': 'Minotaur','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Ninja = {'name': 'Ninja','HP1': 20,'HP2': 30,'HP3': 40,'HP4': 55,'Atk1L1': 4,'Atk1L2': 5,'Atk1L3': 5,'Atk1L4': 7,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 3,'Atk1L4dmg': 3,'Atk2L1': 3,'Atk2L2': 4,'Atk2L3': 5,'Atk2L4': 6,'Atk2L1dmg': 3,'Atk2L2dmg': 4,'Atk2L3dmg': 5,'Atk2L4dmg': 6,'Atk3L1': 2,'Atk3L2': 2,'Atk3L3': 3,'Atk3L4': 3,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 4,'Dfns2': 5,'Dfns3': 6,'Dfns4': 7,}
Ogre = {'name': 'Ogre','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
OozeMonster = {'name': 'Ooze Monster','HP1': 30,'HP2': 40,'HP3': 60,'HP4': 90,'Atk1L1': 4,'Atk1L2': 5,'Atk1L3': 6,'Atk1L4': 7,'Atk1L1dmg': 1,'Atk1L2dmg': 1,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 3,'Atk2L1dmg': 2,'Atk2L2dmg': 2,'Atk2L3dmg': 3,'Atk2L4dmg': 3,'Atk3L1': 1,'Atk3L2': 1,'Atk3L3': 2,'Atk3L4': 2,'Atk3L1dmg': 4,'Atk3L2dmg': 4,'Atk3L3dmg': 6,'Atk3L4dmg': 7,'Dfns1': 1,'Dfns2': 2,'Dfns3': 3,'Dfns4': 3,}
Orc = {'name': 'Orc','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Pirate = {'name': 'Pirate','HP1': 20,'HP2': 30,'HP3': 40,'HP4': 45,'Atk1L1': 4,'Atk1L2': 5,'Atk1L3': 5,'Atk1L4': 6,'Atk1L1dmg': 2,'Atk1L2dmg': 3,'Atk1L3dmg': 3,'Atk1L4dmg': 4,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 3,'Atk2L2dmg': 3,'Atk2L3dmg': 4,'Atk2L4dmg': 5,'Atk3L1': 2,'Atk3L2': 3,'Atk3L3': 3,'Atk3L4': 4,'Atk3L1dmg': 3,'Atk3L2dmg': 4,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 3,'Dfns2': 4,'Dfns3': 4,'Dfns4': 5,}
PossessedDoll = {'name': 'Possessed Doll','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Robot = {'name': 'Robot','HP1': 63,'HP2': 76,'HP3': 84,'HP4': 102,'Atk1L1': 4,'Atk1L2': 5,'Atk1L3': 5,'Atk1L4': 6,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 3,'Atk2L2': 4,'Atk2L3': 4,'Atk2L4': 5,'Atk2L1dmg': 2,'Atk2L2dmg': 3,'Atk2L3dmg': 3,'Atk2L4dmg': 4,'Atk3L1': 2,'Atk3L2': 2,'Atk3L3': 3,'Atk3L4': 3,'Atk3L1dmg': 3,'Atk3L2dmg': 4,'Atk3L3dmg': 5,'Atk3L4dmg': 5,'Dfns1': 2,'Dfns2': 3,'Dfns3': 4,'Dfns4': 4,}
Sasquatch = {'name': 'Sasquatch','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Unicorn = {'name': 'Unicorn','HP1': 25,'HP2': 35,'HP3': 50,'HP4': 85,'Atk1L1': 3,'Atk1L2': 4,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 2,'Atk1L2dmg': 2,'Atk1L3dmg': 3,'Atk1L4dmg': 4,'Atk2L1': 3,'Atk2L2': 4,'Atk2L3': 5,'Atk2L4': 6,'Atk2L1dmg': 3,'Atk2L2dmg': 4,'Atk2L3dmg': 5,'Atk2L4dmg': 5,'Atk3L1': 2,'Atk3L2': 3,'Atk3L3': 4,'Atk3L4': 5,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 6,'Atk3L4dmg': 7,'Dfns1': 4,'Dfns2': 5,'Dfns3': 6,'Dfns4': 7,}
Vampire = {'name': 'Vampire','HP1': 40,'HP2': 50,'HP3': 75,'HP4': 95,'Atk1L1': 4,'Atk1L2': 5,'Atk1L3': 5,'Atk1L4': 6,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 2,'Atk2L2': 3,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 2,'Atk2L2dmg': 3,'Atk2L3dmg': 4,'Atk2L4dmg': 4,'Atk3L1': 1,'Atk3L2': 2,'Atk3L3': 2,'Atk3L4': 3,'Atk3L1dmg': 3,'Atk3L2dmg': 4,'Atk3L3dmg': 5,'Atk3L4dmg': 5,'Dfns1': 3,'Dfns2': 4,'Dfns3': 5,'Dfns4': 6,}
Werewolf = {'name': 'Werewolf','HP1': 25,'HP2': 30,'HP3': 50,'HP4': 75,'Atk1L1': 4,'Atk1L2': 4,'Atk1L3': 5,'Atk1L4': 6,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 3,'Atk1L4dmg': 3,'Atk2L1': 3,'Atk2L2': 3,'Atk2L3': 4,'Atk2L4': 5,'Atk2L1dmg': 2,'Atk2L2dmg': 3,'Atk2L3dmg': 3,'Atk2L4dmg': 3,'Atk3L1': 1,'Atk3L2': 1,'Atk3L3': 2,'Atk3L4': 4,'Atk3L1dmg': 4,'Atk3L2dmg': 4,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 4,'Dfns2': 5,'Dfns3': 6,'Dfns4': 7,}
Witch = {'name': 'Witch','HP1': 0,'HP2': 0,'HP3': 0,'HP4': 0,'Atk1L1': 0,'Atk1L2': 0,'Atk1L3': 0,'Atk1L4': 0,'Atk1L1dmg': 0,'Atk1L2dmg': 0,'Atk1L3dmg': 0,'Atk1L4dmg': 0,'Atk2L1': 0,'Atk2L2': 0,'Atk2L3': 0,'Atk2L4': 0,'Atk2L1dmg': 0,'Atk2L2dmg': 0,'Atk2L3dmg': 0,'Atk2L4dmg': 0,'Atk3L1': 0,'Atk3L2': 0,'Atk3L3': 0,'Atk3L4': 0,'Atk3L1dmg': 0,'Atk3L2dmg': 0,'Atk3L3dmg': 0,'Atk3L4dmg': 0,'Dfns1': 0,'Dfns2': 0,'Dfns3': 0,'Dfns4': 0,}
Wizard = {'name': 'Wizard','HP1': 40,'HP2': 60,'HP3': 90,'HP4': 115,'Atk1L1': 3,'Atk1L2': 3,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 1,'Atk1L2dmg': 1,'Atk1L3dmg': 2,'Atk1L4dmg': 2,'Atk2L1': 2,'Atk2L2': 3,'Atk2L3': 4,'Atk2L4': 4,'Atk2L1dmg': 4,'Atk2L2dmg': 4,'Atk2L3dmg': 5,'Atk2L4dmg': 6,'Atk3L1': 1,'Atk3L2': 1,'Atk3L3': 3,'Atk3L4': 3,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 4,'Dfns2': 5,'Dfns3': 6,'Dfns4': 6,}
Yeti = {'name': 'Yeti','HP1': 30,'HP2': 35,'HP3': 55,'HP4': 80,'Atk1L1': 3,'Atk1L2': 4,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 3,'Atk1L4dmg': 4,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 3,'Atk2L2dmg': 3,'Atk2L3dmg': 4,'Atk2L4dmg': 5,'Atk3L1': 1,'Atk3L2': 1,'Atk3L3': 2,'Atk3L4': 3,'Atk3L1dmg': 4,'Atk3L2dmg': 5,'Atk3L3dmg': 5,'Atk3L4dmg': 6,'Dfns1': 3,'Dfns2': 3,'Dfns3': 4,'Dfns4': 5,}
Zombie = {'name': 'Zombie','HP1': 49,'HP2': 60,'HP3': 90,'HP4': 120,'Atk1L1': 3,'Atk1L2': 3,'Atk1L3': 4,'Atk1L4': 5,'Atk1L1dmg': 1,'Atk1L2dmg': 2,'Atk1L3dmg': 2,'Atk1L4dmg': 3,'Atk2L1': 2,'Atk2L2': 2,'Atk2L3': 3,'Atk2L4': 4,'Atk2L1dmg': 2,'Atk2L2dmg': 3,'Atk2L3dmg': 4,'Atk2L4dmg': 4,'Atk3L1': 1,'Atk3L2': 1,'Atk3L3': 2,'Atk3L4': 3,'Atk3L1dmg': 3,'Atk3L2dmg': 4,'Atk3L3dmg': 4,'Atk3L4dmg': 5,'Dfns1': 1,'Dfns2': 2,'Dfns3': 2,'Dfns4': 3,}

import random

def pickmonster (slotnumber):
    global position
    if slotnumber == 1: position = Cyclops
    if slotnumber == 2: position = Genie
    if slotnumber == 3: position = GiantApe
    if slotnumber == 4: position = GiantLizard
    if slotnumber == 5: position = GreyAlien
    if slotnumber == 6: position = Leprechaun
    if slotnumber == 7: position = Medusa
    if slotnumber == 8: position = Ninja
    if slotnumber == 9: position = OozeMonster
    if slotnumber == 10: position = Pirate
    if slotnumber == 11: position = Robot
    if slotnumber == 12: position = Unicorn
    if slotnumber == 13: position = Vampire
    if slotnumber == 14: position = Werewolf
    if slotnumber == 15: position = Wizard
    if slotnumber == 16: position = Zombie

pickednumbers = []
def slotseeder ():
    global randomnumber
    randomnumber = (random.randrange(1,16))
    if randomnumber in pickednumbers:
        t = 1
    else:
        pickednumbers.append(randomnumber)
        pickmonster (randomnumber)
slotseeder
P01 = position
print P01

4 个答案:

答案 0 :(得分:5)

代码中存在几个问题。就像你没有调用函数一样,如果它后面没有开括号和右括号。

即:不是slotseeder而是slotseeder()

(那是打破代码的那个)

我可能会编写如下示例代码:

### Monster Statistics ####

default_stats = dict(name='unknown',
                 HP=[0, 0, 0, 0],
                 Atk1=[0, 0, 0, 0], Atk1dmg=[0, 0, 0, 0],
                 Atk2=[0, 0, 0, 0], Atk2dmg=[0, 0, 0, 0],
                 Atk3=[0, 0, 0, 0], Atk3dmg=[0, 0, 0, 0],
                 Dfns=[0, 0, 0, 0])

Monsters = [dict(default_stats, name='Cerebus'),
            dict(default_stats, name='Cthulhu'),
            dict(default_stats, name='Cyclops',
                 HP=[65, 85, 95, 130],
                 Atk1=[3, 4, 4, 5], Atk1dmg=[3, 4, 4, 5],
                 Atk2=[2, 3, 3, 4], Atk2dmg=[5, 6, 6, 7],
                 Atk3=[1, 2, 3, 3], Atk3dmg=[5, 6, 6, 7],
                 Dfns=[2, 3, 4, 5]),
            dict(default_stats, name='Genie'),
            dict(default_stats, name='Giant Ape',
                 HP=[70, 90, 110, 140],
                 Atk1=[3, 4, 4, 5], Atk1dmg=[4, 5, 5, 6],
                 Atk2=[2, 2, 3, 4], Atk2dmg=[5, 6, 6, 7],
                 Atk3=[1, 2, 3, 3], Atk3dmg=[6, 6, 7, 8],
                 Dfns=[2, 3, 3, 4]),
            dict(default_stats, name='Giant Lizard',
                 HP=[80, 100, 130, 170],
                 Atk1=[3, 3, 4, 5], Atk1dmg=[3, 4, 5, 6],
                 Atk2=[2, 3, 3, 4], Atk2dmg=[4, 5, 6, 7],
                 Atk3=[1, 2, 2, 3], Atk3dmg=[6, 7, 8, 8],
                 Dfns=[1, 2, 2, 3]),
            dict(default_stats, name='Grey Alien',
                 HP=[30, 40, 45, 50],
                 Atk1=[3, 3, 4, 5], Atk1dmg=[1, 2, 2, 3],
                 Atk2=[2, 2, 3, 4], Atk2dmg=[2, 3, 3, 4],
                 Atk3=[1, 2, 3, 4], Atk3dmg=[4, 5, 5, 6],
                 Dfns=[3, 4, 4, 5]),
            dict(default_stats, name='Gryffin'),
            dict(name='Leprechaun',
                HP=[30, 35, 50, 65],
                Atk1=[3, 4, 4, 5], Atk1dmg=[1, 1, 2, 3],
                Atk2=[2, 3, 4, 5], Atk2dmg=[2, 2, 3, 4],
                Atk3=[1, 2, 3, 4], Atk3dmg=[4, 5, 6, 6],
                Dfns=[4, 6, 7, 8]),
            dict(default_stats, name='Medusa',
                 HP=[20, 30, 45, 50],
                 Atk1=[3, 3, 4, 4], Atk1dmg=[1, 2, 2, 3],
                 Atk2=[2, 2, 3, 4], Atk2dmg=[2, 2, 3, 3],
                 Atk3=[1, 1, 2, 3], Atk3dmg=[4, 5, 5, 6],
                 Dfns=[2, 3, 4, 4]),
            dict(default_stats, name='Minotaur'),
            dict(default_stats, name='Ninja',
                 HP=[20, 30, 40, 55],
                 Atk1=[4, 5, 5, 7], Atk1dmg=[1, 2, 3, 3],
                 Atk2=[3, 4, 5, 6], Atk2dmg=[3, 4, 5, 6],
                 Atk3=[2, 2, 3, 3], Atk3dmg=[4, 5, 5, 6],
                 Dfns=[4, 5, 6, 7]),
            dict(default_stats, name='Ogre'),
            dict(default_stats, name='Ooze Monster',
                 HP=[30, 40, 60, 90],
                 Atk1=[4, 5, 6, 7], Atk1dmg=[1, 1, 2, 3],
                 Atk2=[2, 2, 3, 3], Atk2dmg=[2, 2, 3, 3],
                 Atk3=[1, 1, 2, 2], Atk3dmg=[4, 4, 6, 7],
                 Dfns=[1, 2, 3, 3]),
            dict(default_stats, name='Orc'),
            dict(default_stats, name='Pirate',
                 HP=[20, 30, 40, 45],
                 Atk1=[4, 5, 5, 6], Atk1dmg=[2, 3, 3, 4],
                 Atk2=[2, 2, 3, 4], Atk2dmg=[3, 3, 4, 5],
                 Atk3=[2, 3, 3, 4], Atk3dmg=[3, 4, 5, 6],
                 Dfns=[3, 4, 4, 5]),
            dict(default_stats, name='Possessed Doll'),
            dict(default_stats, name='Robot',
                HP=[63, 76, 84, 102],
                Atk1=[4, 5, 5, 6], Atk1dmg=[1, 2, 2, 3],
                Atk2=[3, 4, 4, 5], Atk2dmg=[2, 3, 3, 4],
                Atk3=[2, 2, 3, 3], Atk3dmg=[3, 4, 5, 5],
                Dfns=[2, 3, 4, 4]),
            dict(default_stats, name='Sasquatch'),
            dict(name='Unicorn',
                 HP=[25, 35, 50, 85],
                 Atk1=[3, 4, 4, 5], Atk1dmg=[2, 2, 3, 4],
                 Atk2=[3, 4, 5, 6], Atk2dmg=[3, 4, 5, 5],
                 Atk3=[2, 3, 4, 5], Atk3dmg=[4, 5, 6, 7],
                 Dfns=[4, 5, 6, 7]),
            dict(default_stats, name='Vampire',
                 HP=[40, 50, 75, 95],
                 Atk1=[4, 5, 5, 6], Atk1dmg=[1, 2, 2, 3],
                 Atk2=[2, 3, 3, 4], Atk2dmg=[2, 3, 4, 4],
                 Atk3=[1, 2, 2, 3], Atk3dmg=[3, 4, 5, 5],
                 Dfns=[3, 4, 5, 6]),
            dict(default_stats, name='Werewolf',
                HP=[25, 30, 50, 75],
                Atk1=[4, 4, 5, 6], Atk1dmg=[1, 2, 3, 3],
                Atk2=[3, 3, 4, 5], Atk2dmg=[2, 3, 3, 3],
                Atk3=[1, 1, 2, 4], Atk3dmg=[4, 4, 5, 6],
                Dfns=[4, 5, 6, 7]),
            dict(default_stats, name='Witch'),
            dict(default_stats, name='Wizard',
                 HP=[40, 60, 90, 115],
                 Atk1=[3, 3, 4, 5], Atk1dmg=[1, 1, 2, 2],
                 Atk2=[2, 3, 4, 4], Atk2dmg=[4, 4, 5, 6],
                 Atk3=[1, 1, 3, 3], Atk3dmg=[4, 5, 5, 6],
                 Dfns=[4, 5, 6, 6]),
            dict(default_stats, name='Yeti',
                 HP=[30, 35, 55, 80],
                 Atk1=[3, 4, 4, 5], Atk1dmg=[1, 2, 3, 4],
                 Atk2=[2, 2, 3, 4], Atk2dmg=[3, 3, 4, 5],
                 Atk3=[1, 1, 2, 3], Atk3dmg=[4, 5, 5, 6],
                 Dfns=[3, 3, 4, 5]),
            dict(default_stats, name='Zombie',
                 HP=[49, 60, 90, 120],
                 Atk1=[3, 3, 4, 5], Atk1dmg=[1, 2, 2, 3],
                 Atk2=[2, 2, 3, 4], Atk2dmg=[2, 3, 4, 4],
                 Atk3=[1, 1, 2, 3], Atk3dmg=[3, 4, 4, 5],
                 Dfns=[1, 2, 2, 3])
    ]

import random

picked = [monster for monster in Monsters 
          if monster['name'] in ['Cyclops',
                                 'Genie', 'Giant Ape', 'Giant Lizard',
                                 'Grey Alien', 'Leprechaun', 'Medusa',
                                 'Ninja', 'Ooze Monster', 'Pirate',
                                 'Robot', 'Unicorn', 'Vampire',
                                 'Werewolf', 'Wizard', 'Zombie']]

def slotseeder ():
    randomnumber = (random.randrange(0,len(picked)))
    if picked[randomnumber] != None:
        pickedmonster = Monsters[randomnumber]
        picked[randomnumber] = None
        return pickedmonster

P01 = slotseeder()
if P01 is not None:
    print "I got monster:", P01
else:
    print "Monster already used"

如您所见,主要变化在于数据结构。

  • 将所有怪物放在一个唯一的列表中。为每个变量名使用不同的变量名是没有意义的。当你想在以后挑选一个怪物时,它只会使代码更难写。
  • 我更改了dict初始化语法,另一个使用了太多的引号来满足我的口味。
  • 还添加了默认字典,方便显示哪些怪物真正被定义,如果一些怪物足够接近,也可以用作模板。
  • 用真实列表替换了编号变量(如HP1,HP2,HP3等)的列表。这就是列表的设计目标。这略微改变了访问语法,但它开启了新的可能性。对于Atk级别可能会进行相同类型的更改,因为还有编号变量,但名称也表明Atk1和Atk1dmg可以组合为元组。我把它留给读者来继续增强结构。

我相信结果比初始结果更容易阅读。

这只是一种可能性。另一种可能是使用带有怪物名称的字典作为所有怪物的关键。它可能比上面的更好,并且定义一个真正的Monster类可能比使用dict()更好。但每天只需一步即可。

更改数据结构后,对代码的更改很容易。我告诉你弄明白了。关于初始程序的最后一个细节:它永远不会有一个Zombie(因为randrange行为)。我认为这是一个错误并改变了它。

使用python玩得开心: - )

注意:如果您想知道我是否真的编辑了这个怪诞的初始结构,答案是 no 。我写了一个程序来为我做。好吧,我为此目的使用了一些内联Perl,因为它比使用Python编写它更快。我知道。我是邪恶的。

答案 1 :(得分:1)

我建议将ifs写成:

monsters = [Cyclops, Genie, GiantApe, GiantLizard, GreyAlien, Leprechaun, Medusa, Ninja, OozeMonster, Pirate, Robot, Unicorn, Vampire, Werewolf, Wizard, Zombie]
monster = monsters[randomnumber-1]

答案 2 :(得分:0)

我发现你根本没有在外部范围内声明position。在函数声明之前声明position,你应该没问题。

答案 3 :(得分:0)

要使用全局变量,首先要全局创建它(在所有类和函数之外),然后在本地将其声明为全局(使用global关键字),以便分配给它。

以下是您的代码缩短版本:

>>> Cyclops = "data for Cyclops"
... position = "" # declares position in global scope
... def pickmonster ():
...     global position # tells python that the local variable 'position' refers to the global 'position'
...     position = Cyclops
... pickmonster()
... print position
data for Cyclops

请注意,通常不赞成使用全局变量。最好让函数pickmonster返回您需要的数据并在本地跟踪它。