这个错误是怎么回事:“Int 对象没有属性“strengthPts”?

问题描述 投票:0回答:1

我正在为我的 Python 入门课程创建一些简单的代码。但是,我一直在第 12 行收到属性错误:“AttributeError:‘int’对象没有属性‘strengthPts’”。这是我的代码:

class superhero:
    def __init__(self, name = "", strengthPts = 0, motto = "", strength = ""):
        self.name = name
        self.strengthPts = int(strengthPts)
        self.motto = motto
        self.strenght = strength
    def addStrengthPts(self, points):
        self.strengthPts = self.strengthPts + points
def main():
    s = superhero("Señor Cement", 0, "These streets won\'t stop me!", "creating cement from matter around him and using it to halt villains.")
    superhero.addStrengthPts(0,5)
    print("Today on Super of the Week, we present to you" + s.name + "After taking down a villain," + s.name + "gained" + str(s.strengthPts) + "points! With their power of" + s.strength + "and the awesome motto,\"" + s.motto + " you\'ll definitely want to give their comic a read!")

main()

如您所见,我在第 10 行将类函数 superhero 分配给 s,我认为问题可能出在我错误地引用了类。此外,我尝试将 strengthPts 重新分配为整数,相信 python 可能不会将参数读取为一个。但是,这些都没有用,我只是不断收到同样的错误。有什么想法吗?

python attributes typeerror
1个回答
0
投票

您将两个参数传递给

addStrengthPoints

superhero.addStrengthPts(0,5)

你应该只传递一个值。 Python 自动将对象

superhero
作为
self
参数传递。

© www.soinside.com 2019 - 2024. All rights reserved.