[英]How do I properly define an object using a class?
我是 Python 的新手。 在来这里询问之前已经搜索了几个小时以了解为什么会发生这种情况。 请帮助我了解如何正确使用 class / object。 预先感谢您的帮助和耐心。
这是错误
第 46 行,在
print("大脑:", hero.brains)
AttributeError:“超级英雄”object 没有属性“大脑”
#Importing Random Module
import random
#create Superhero class
class Superhero():
#initializing our class and setting its atrributes
def _init_(self):
self.superName = superName
self.power = power
self.braun = braun
self.brains = brains
self.stamina = stamina
self.wisdom = wisdom
self.constitution = constitution
self.dexterity = dexterity
self.speed = speed
# adding random values to each
braun = random.randint(1,20)
brains = random.randint(1,20)
stamina = random.randint(1,20)
wisdom = random.randint(1,20)
constitution = random.randint(1,20)
dexterity = random.randint(1,20)
speed = random.randint(1,20)
print("Please enter your Super Hero name: ")
# creating the super hero object
hero = Superhero()
# assigning a value to superName using the user's input
hero.superName = input('>')
# print the result of the created object, including its parameters
print("Your name is %s. " % (hero.superName))
print("Your new stats are: ")
print("")
print("Brains: ", hero.brains)
print("Braun: ", hero.braun)
print("Stamina: ", hero.stamina)
print("Wisdom: ", hero.wisdom)
print("Constitution: ", hero.constitution)
print("Dexterity: ", hero.dexterity)
print("Speed: ", hero.speed)
print("")
你需要告诉 SuperHero() 英雄的属性是什么。 您在 class 之外定义它们只会创建与 class 具有 0 关系的变量。 这是我建议做的事情:
hero = SuperHero(superName = input("Please enter your Super hero name:"),brains = random.randint(1,20)
然后您可以打印分配给您刚刚创建的 Object “英雄”的属性:
print("Your name is {}".format(hero.superName))
可以肯定的是,这是您的完整代码吗? 因为这里的问题很简单。 您忘记将参数添加到 class 构造函数中。 您为统计数据分配了随机数,但它们不在 object scope 中,这意味着您的超级英雄不会访问这些变量,除非您将它们作为 arguments 传递给您的构造函数。
有两种方法可以解决这个问题:
A)您在构造函数中将它们作为 arguments 传递,所以
class Superhero():
#initializing our class and setting its atrributes
def _init_(self, superName, power, braun, brains, stamina, wisdom, constitution, dexterity, speed):
self.superName = superName
self.power = power
self.braun = braun
self.brains = brains
self.stamina = stamina
self.wisdom = wisdom
self.constitution = constitution
self.dexterity = dexterity
self.speed = speed
print("Please enter your Super Hero name: ")
superName = input('>')
power = random.randint(1,20)
braun = random.randint(1,20)
brains = random.randint(1,20)
stamina = random.randint(1,20)
wisdom = random.randint(1,20)
constitution = random.randint(1,20)
dexterity = random.randint(1,20)
speed = random.randint(1,20)
hero = Superhero(superName, power, braun, brains, stamina, wisdom, constitution, dexterity, speed)
然后你可以打印任何你想要的东西。
B)由于您的统计信息都是随机的,您可以告诉构造函数将它们分配给随机数,这会将您的代码缩短几行,但不会让您在创建 object 时分配自己的统计信息,如下所示:
class Superhero():
#initializing our class and setting its atrributes
def _init_(self, superName):
self.superName = superName
self.power = random.randint(1,20)
self.braun = random.randint(1,20)
self.brains = random.randint(1,20)
self.stamina = random.randint(1,20)
self.wisdom = random.randint(1,20)
self.constitution = random.randint(1,20)
self.dexterity = random.randint(1,20)
self.speed = random.randint(1,20)
print("Please enter your Super Hero name: ")
superName = input('>')
hero = Superhero(superName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.