小编Lia*_*yes的帖子

Python 错误:缺少 1 个必需的位置参数:'self'

我是 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)

python class self

5
推荐指数
1
解决办法
3万
查看次数

在创建存储库后,如何选择 git ignore Node.js?

我做了一个新的存储库。这是一个 JavaScript/HTML/CSS 项目。

当我在 GitHub.com 个人资料上创建目录时,我忘记从 git ignore 下拉菜单中选择 git ignore Node.js。

在我已经创建了存储库之后,有没有办法让我忽略 Node.js?

谢谢你的帮助。

javascript git ignore github node.js

0
推荐指数
2
解决办法
5285
查看次数

标签 统计

class ×1

git ×1

github ×1

ignore ×1

javascript ×1

node.js ×1

python ×1

self ×1