class Personaje:
def__init__(self,name):
self.name=pepe
self.type=warrior
self.health=100
def eat(self,food):
if(food=="manzana"):
self.health-=10
elif(food=="leche"):
self.health+=5
else(food=="mondongo"):
self.health+= int(0.0001)
我得到无效的语法在(自身名称):<;<;
在
Tags:
问题1
看看错误:
在
^{pr2}$def
和__init__
之间应该有一个空格,因此__init__
函数的定义应该是:问题2
else语句不采用}do之类的表达式,因此会导致以下语法错误:
if
或{else
表示其他所有内容,因此如果您希望它只适用于“mondongo”,则应该在那里使用另一个elif
。在问题3
函数
eat
在__init__
函数内定义,导致:如果取消插入函数(向左移动4个空格),则eat将在类中定义,而不是在init函数中定义。所以基本结构应该这样缩进:
问题4
未将Personaje名称设置为
__init__
函数中指定的名称。如果您希望默认名称为pepe
,并键入warrior
,我建议您将init函数改为如下所示:您的最后一个
Personaje
类现在应该如下所示:相关问题 更多 >
编程相关推荐