简体   繁体   中英

Global Variable Not Defined

I'm calling two separate functions to determine what "P01" equals. The first one selects a random number, and discards random numbers already picked. The second one takes the result of the random number and picks a variable to make 'position' equal. I then say that 'P01' equals 'position.'

I've made 'position' a global variable, but I keep getting an error when I try to assign 'position' to 'P01' saying "position is not defined."

Any ideas?

### 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

There is several problems in the code. Like you are not calling function if it is not followed by opening and closing parenthesis.

ie: not slotseeder but slotseeder()

(That is the one that breaks the code)

I would probably write your sample code as below:

### 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"

As you can see, the main change is in the data structure.

  • put all the monsters in one unique list. There is no point in using a different variable name for each. It just makes code harder to write when you want to pick a monster later.
  • I changed the dict initialisation syntax, the other one uses too much quotes for my taste.
  • also added default dictionary, it is handy to show which monsters are really defined or not, could also be used as template if some monsters are close enough.
  • replaced lists of numbered variables (like HP1, HP2, HP3, etc) with real lists. That's what lists are designed for. That slightly change access syntax, but it opens new possibilities. The same kind of change can probably be done for Atk levels as there is also numbered variables, but names suggests also that Atk1 and Atk1dmg may be combined as a tuple. I leave it to reader to still enhance the structure.

I believe the result is much easier to read than the initial one.

That is just a possibility. Another one could be to use a dictionary with monster name as key for all monsters. It would probably be even better than the above, and also defining a real Monster class would probably be a better idea than using dict(). But one step every day is enough.

After changing the data structure, changes to code are easy. I leave you figure it out. Just one last detail about initial program: it would never got a Zombie (because of randrange behavior). I figured it was a bug and changed it.

Have fun with python :-)

note : If you wonder if I really edited the monstruous initial structure, the answer is no . I wrote a program to do it for me. Well, I used some inline Perl for that purpose as it was faster than writing it using Python. I know. I'm evil.

May I suggest writing the ifs as something like:

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

I see you didn't declare position in the outer scope at all. Declare position before the function declarations, and you should be ok.

To use a global variable, first create it globally (outside of all classes and functions), then declare it as global locally (with global keyword) so you can assign to it.

Here's a shortened version of your code that does it right:

>>> 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

Do note that use of global variables is generally frowned upon. It would be better to have the function pickmonster return the data you need and keep track of it locally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM