Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving an issue with classes
    primarykey
    data
    text
    <p>I am trying to convert a function I made for computing a single "damage" output and I don't understand where I'm going wrong. As a function it processes my methods normally but when made into a class I receive a global name not found. Also, I'm thinking that my <strong>init</strong> is correct but I may be wrong.</p> <pre><code>import random class Damage(object): """Represents 1 single damage value""" def __init__(self): """Constructor initializes objects attributes. stats.""" ### I need to initialize my stats from a different class since they ### are not a constant. self._statList is a dictionary that will be ### imported. self._statList = {"strength":100, "hitStat":90, "eBlockchance":10, \ "critStat":15, "defense":0} self._strength = self._statList["strength"] self._hitStat = self._statList["hitStat"] self._eBlockchance = self._statList["eBlockchance"] self._defense = self._statList["defense"] self._critStat = self._statList["critStat"] def __str__(self): """Returns string representation of the damage""" self._base = self._strength - self._defense self._damage = random.randint(self._base/2, self._base) ### I have no idea why this isn't running my methods below. ### NameError: global name 'hitChance' is not defined. if hitChance() == True: return "Miss" else: if enemyBlock() == True: return "Block" else: if critChance() == True: return str(self._damage*2) + "CRITICAL" else: return str(self._damage) def critChance(self): self._chance = random.randint(1,100) if self._chance &lt;= self._critStat: return True def enemyBlock(self): self._chance = random.randint(1,100) if self._chance &lt;= self._eBlockchance: return True def hitChance(self): self._chance = random.randint(1,100) if self._chance &gt; self._hitStat: return True </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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