我有一个python类,必须在另一个python文件中使用一个函数。我导入了类,并且没有导入错误但是它给出了关于属性不存在的错误
我的文件是combact.py和monster.py。我已经进口了战斗。
这是combact.py
import random
class Combat():
dodge_limit = 6
attack_limit = 6
def dodge(self):
roll = random.randint(1,self.dodge_limit)
return roll > 4
def attack(self):
roll = random.randint(1,self.attack_limit)
return roll > 4
这是monster.py
import random
from combact import Combat
COLOR = ['blue', 'red', 'green', 'yellow']
class Monsters(Combat):
max_hitpoints = 1
min_hitpoints = 1
max_experience = 1
min_experience = 1
weapon='sword'
sound = 'roar'
def __init__(self, **kwargs):
self.hit_points = random.randint(self.min_hitpoints,self.max_hitpoints)
self.color = random.choice(COLOR)
self.experience = random.randint(self.min_experience,self.max_experience)
for key,value in kwargs.items():
setattr(self,key,value)
def __str__(self):
return '{} {} HP: {} XP: {}'.format(self.color.title(),self.__class__.__name__,self.hit_points,self.experience)
def battlecry(self):
return self.sound.upper()
class Goblin(Monsters):
max_experience=2
min_hitpoints = 2
max_hitpoints = 3
sound = 'Squek'
class Troll(Monsters):
max_experience = 5
min_experience = 2
max_hitpoints = 8
min_hitpoints = 5
sound = 'Wiggling'
class Dragon(Monsters):
max_hitpoints = 10
min_hitpoints = 7
max_experience = 10
min_experience = 8
weapon = 'fire'
sound = 'raaaaaaar'
当我运行monsters.py时输出为:
这是另一个带有相同导入但正在工作的文件character.py
如果你需要character.py
from combact import Combat
class Character(Combat):
experience = 0
hit_points = 10
def get_weapon(self):
wep = input("Choose Your Weapon.([S]word , [A]xe , [B]ow)").lower()
if wep in 'sab':
if wep == 's':
return 'sword'
elif wep == 'a':
return 'axe'
else:
return 'Bow'
else:
print("Weapon not in you arsenal right now")
return self.get_weapon()
def __init__(self,**kwargs):
self.name = input("Enter Name:")
self.weapon = self.get_weapon()
for key,value in kwargs.items():
setattr(self,key,value)
请帮助!!!我是菜鸟和学习者。 谢谢!!