Я пытаюсь преобразовать функцию, которую я сделал для вычисления одного «повреждения», и я не понимаю, где я ошибаюсь. Как функция, он обрабатывает мои методы нормально, но когда я превращен в класс, я получаю глобальное имя, которое не найдено. Кроме того, я думаю, что мой init верен, но я могу ошибаться.Имея проблему с классами
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 <= self._critStat:
return True
def enemyBlock(self):
self._chance = random.randint(1,100)
if self._chance <= self._eBlockchance:
return True
def hitChance(self):
self._chance = random.randint(1,100)
if self._chance > self._hitStat:
return True
Пожалуйста, включите полную трассу, и мы не будем догадываться о том, какое имя вы можете пропустить. –
где проблема? на каком методе (функции) возникает ошибка? – Barranka
Теперь я вижу, что вы помещаете сообщение об ошибке в качестве комментария в примере кода. Это нам не очень хорошо видно. Полная трассировка * отдельно * сообщила бы нам быстрее. –