Python error: missing 1 required positional argument: 'self'
本问题已经有最佳答案,请猛点这里访问。
我对python是个新手,我正在努力学习如何使用类。有人知道这怎么不管用吗?任何关于关键字"self"的额外提示都将非常感谢。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Enemy: life = 3 def attack(self): print('ouch!') self.life -= 1 def checkLife(self): if self.life <= 0: print('I am dead') else: print(str(self.life) +"life left") enemy1 = Enemy enemy1.attack() enemy1.checkLife() |
错误:
1 2 3 4 5 6 7 | C:\Users\Liam\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Liam/PycharmProjects/YouTube/first.py Traceback (most recent call last): File"C:/Users/Liam/PycharmProjects/YouTube/first.py", line 16, in <module> enemy1.attack() TypeError: attack() missing 1 required positional argument: 'self' Process finished with exit code 1 |
(P)字母名称EDOCX1是一个很好的例子你需要开始的阶级,(p)字母名称