如何定义这个超级英雄类,然后创建/使用它的实例

2024-06-07 03:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,在这里我非常努力地理解这个问题。 我问了之前的一个问题,得到了几个答案,尝试使用每个解决方案,但没有一个有效。 如果可以的话,请帮忙

***我被要求在我的问题中添加更多文本以便发布,因此请忽略此区域。lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum

错误消息

Please enter your Super Hero name: 
>PleaseHelpMan
Traceback (most recent call last):
  File "/Users/colinbarclay/Desktop/NEW VERSION of SuperHero Classes..py", line 39, in <module>
    hero = Superhero(superName, power, braun, brains, stamina, wisdom, constitution, dexterity, speed)
TypeError: Superhero() takes no arguments
>>> 

代码

#Importing Random Module

import random

#create Superhero class

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: ")

# assigning a value to superName using the user's input
superName = input('>')

# adding random values to each

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)


# creating the super hero object
hero = Superhero(superName, power, braun, brains, stamina, wisdom, constitution, dexterity, speed)

# 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("")

Tags: selfrandomdexterityspeedprintrandintwisdomipsum