Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is several problems in the code. Like you are not calling function if it is not followed by opening and closing parenthesis.</p> <p>ie: not <code>slotseeder</code> but <code>slotseeder()</code></p> <p>(That is the one that breaks the code)</p> <p>I would probably write your sample code as below:</p> <pre><code>### 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" </code></pre> <p>As you can see, the main change is in the data structure.</p> <ul> <li>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.</li> <li>I changed the dict initialisation syntax, the other one uses too much quotes for my taste.</li> <li>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.</li> <li>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.</li> </ul> <p>I believe the result is much easier to read than the initial one.</p> <p>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.</p> <p>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.</p> <p>Have fun with python :-)</p> <p><strong>note</strong>: If you wonder if I really edited the monstruous initial structure, the answer is <em>no</em>. 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload