我是 Python 新手,我正在尝试学习如何使用类。有谁知道为什么这不起作用?任何有关关键字“self”的其他提示将不胜感激。
代码:
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()
Run Code Online (Sandbox Code Playgroud)
错误:
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
Run Code Online (Sandbox Code Playgroud) 我做了一个新的存储库。这是一个 JavaScript/HTML/CSS 项目。
当我在 GitHub.com 个人资料上创建目录时,我忘记从 git ignore 下拉菜单中选择 git ignore Node.js。
在我已经创建了存储库之后,有没有办法让我忽略 Node.js?
谢谢你的帮助。