TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错

报错Can't instantiate abstract class Ultraman with abstract methods sttack

通过非常仔细的排查,发现错误如下:

1、单词拼写错误是诱发这个报错的直接原因

 

  1 from abc import ABCMeta,abstractmethod
  2 from random import randint,randrange
  3 
  4 class Fighter(object,metaclass=ABCMeta):
  5     """战斗者"""
  6 
  7     #通过__slots__魔法限定对象可以绑定的成员变量
  8     __slots__ = ('_name','_hp')
  9 
 10     def __init__(self,name,hp):
 11         """
 12         初始化方法
 13         :param name: 名字
 14         :param hp: 战斗值
 15         """
 16         self._name = name
 17         self._hp = hp
 18 
 19     @property
 20     def name(self):
 21         return self._name
 22 
 23     @property
 24     def hp(self):
 25         return self._hp
 26 
 27     @hp.setter
 28     def hp(self,hp):
 29         self._hp = hp if hp >= 0 else 0
 30 
 31     @property
 32     def alive(self):
 33         return self._hp > 0
 34 
 35     @abstractmethod
 36     def sttack(self,other):
 37         """
 38         攻击
 39         :param other: 被攻击的对象
 40         :return:
 41         """
 42         pass
 43 
 44 class Ultraman(Fighter):
 45     """奥特曼"""
 46     __slots__ = ('_name',"_hp","_mp")
 47 
 48     def __init__(self,name,hp,mp):
 49         """
 50         初始化名字
 51         :param name: 名字
 52         :param hp: 生命值
 53         :param mp: 魔术值
 54         """
 55         super().__init__(name,hp)
 56         self._mp = mp
 57 
 58     def attack(self,other):
 59         other.hp -= randint(15,25)
 60 
 61     def huge_attack(self,other):
 62         """
 63         穷极必杀技(打掉对方至少50点或3/4的血)
 64         :param other: 被攻击对象
 65         :return: 使用成功返回True否则返回False
 66         """
 67 
 68         if self._mp >= 50:
 69             self._mp -= 50
 70             injury = other.hp * 3//4
 71             injury = injury if injury >= 50 else 50
 72             other.hp -= injury
 73             return True
 74         else:
 75             self.attack(other)
 76             return False
 77 
 78     def magic_attack(self,others):
 79         """
 80         魔法攻击
 81         :param others: 被攻击的群体
 82         :return: 使用魔法成功返回TRUE否则返回False
 83 
 84         """
 85         if self._mp >= 20:
 86             self._mp -= 20
 87             for temp in others:
 88                     if temp.alive:
 89                         temp.hp -= randint(10,15)
 90             return True
 91         else:
 92             return False
 93 
 94     def resume(self):
 95         """恢复魔法值"""
 96         incr_point = randint(1,10)
 97         self._mp += incr_point
 98         return incr_point
 99 
100     def __str__(self):
101         return '~~~%s奥特曼~~~\n'%self._name + \
102             '生命值:%d\n'%self._hp + \
103             '魔法值:%d\n'%self._mp
104 
105 class Monster(Fighter):
106     """小怪兽"""
107 
108     __slots__ = ('_name','_hp')
109 
110     def attack(self,other):
111         other.hp -= randint(10,20)
112 
113     def __str__(self):
114         return '~~~%s小怪兽~~~\n' % self._name + \
115             '生命值:%d\n'%self._hp
116 
117 def is_any_alive(monsters):
118     """判断有没有活着的小怪兽"""
119     for monster in monsters:
120         if monster.alive > 0:
121             return True
122     return False
123 
124 def select_alive_one(monsters):
125     """选中一只活着的小怪兽"""
126     monsters_len = len(monsters)
127     while True:
128         index = randrange(monsters_len)
129         monster = monsters[index]
130         if monster.alive > 0:
131             return monster
132 
133 def display_info(ultraman,monsters):
134     """显示奥特曼和小怪兽的信息"""
135     print(ultraman)
136     for monster in monsters:
137         print(monster,end = '')
138 
139 def main():
140     u = Ultraman('骆昊',1000,120)
141     m1 = Monster('狄仁杰',250)
142     m2 = Monster('白元芳',500)
143     m3 = Monster('张大头',750)
144     ms = [m1,m2,m3]
145     fight_round = 1
146     while u.alive and is_any_alive(ms):
147         print('=======第%02d回合======='%fight_round)
148         m = select_alive_one(ms) #选中一个小怪兽
149         skill = randint(1,10) #通过随机数选择使用哪种技能
150         if skill <= 6:#60%的概率使用普通攻击
151             print('%s使用普通攻击打了%s'%(u.name,m.nama))
152             u.attack(m)
153             print('%s的魔法值恢复了%d点'%(u.name,u.resume()))
154         elif skill <= 9:
155             if u.magic_attack(ms):
156                 print('%s使用了魔法攻击.'%u.name)
157             else:
158                 print('%s使用魔法失败.'%u.name)
159     else:
160         if u.huge_attack(m):
161        

你可能感兴趣的:(TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错)