python - 如何定义这个 Superhero 类,然后创建/使用它的实例
问题描述
我是 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 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("")
解决方案
_init_
应该有两组下划线,而不是一组。所以,应该是__init__
https://www.w3schools.com/python/python_classes.asp
推荐阅读
- rally - 自定义 Rally 全局 API 密钥
- javascript - 当我将鼠标悬停在同一个元素上时,我希望它变暗 - Vanilla JS
- vue.js - 如何禁用 b 表中整列的行单击事件?
- kotlin - 如何强制 Vertx Rx CodeGen 生成 MayBe 作为返回类型
- python - 绘制一组熊猫数据框
- sql - 将具有两个连接计数子查询的 sql 语句转换为 Linq-EF6 函数
- puppet - 检查文件是否存在的 Puppet 模块
- powershell - 在 powershell 搜索中没有得到预期的结果
- entity-framework-6 - Entity Framework 6 使用反射排序
- mapbox - Mapbox GL JS - 设置数据