无效的python冒号错误语法
(self.name)上的语法无效:问题1 请看一下错误:无效的python冒号错误语法,python,Python,(self.name)上的语法无效:问题1 请看一下错误: 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
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)
问题2
else语句不采用任何类似于if
或elif
do的表达式,因此会导致以下语法错误:
def __init__(self, name):
# ....
如果取消该函数(向左移动4个空格),则eat将在类中定义,而不是在init函数中定义。所以基本结构应该缩进如下:
<ipython-input-22-9789ced9c556> in <module>()
----> 1 p.eat('leche')
AttributeError: Personaje instance has no attribute 'eat'
class Personaje:
def __init__(...):
pass
def eat(...):
pass
def __init__(self, name="pepe", type="warrior"):
self.name = name
self.type = type
self.health = 100
问题4
未将Personaje名称设置为\uuuu init\uuu
函数中指定的名称。如果您想将默认名称设置为pepe
并键入warrior
,我建议您将init函数更改为如下所示:
<ipython-input-22-9789ced9c556> in <module>()
----> 1 p.eat('leche')
AttributeError: Personaje instance has no attribute 'eat'
class Personaje:
def __init__(...):
pass
def eat(...):
pass
def __init__(self, name="pepe", type="warrior"):
self.name = name
self.type = type
self.health = 100
您的最后一个Personaje
类现在应该如下所示:
<ipython-input-22-9789ced9c556> in <module>()
----> 1 p.eat('leche')
AttributeError: Personaje instance has no attribute 'eat'
class Personaje:
def __init__(...):
pass
def eat(...):
pass
def __init__(self, name="pepe", type="warrior"):
self.name = name
self.type = type
self.health = 100
问题1
请看一下错误:
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)
问题2
else语句不采用任何类似于if
或elif
do的表达式,因此会导致以下语法错误:
def __init__(self, name):
# ....
如果取消该函数(向左移动4个空格),则eat将在类中定义,而不是在init函数中定义。所以基本结构应该缩进如下:
<ipython-input-22-9789ced9c556> in <module>()
----> 1 p.eat('leche')
AttributeError: Personaje instance has no attribute 'eat'
class Personaje:
def __init__(...):
pass
def eat(...):
pass
def __init__(self, name="pepe", type="warrior"):
self.name = name
self.type = type
self.health = 100
问题4
未将Personaje名称设置为\uuuu init\uuu
函数中指定的名称。如果您想将默认名称设置为pepe
并键入warrior
,我建议您将init函数更改为如下所示:
<ipython-input-22-9789ced9c556> in <module>()
----> 1 p.eat('leche')
AttributeError: Personaje instance has no attribute 'eat'
class Personaje:
def __init__(...):
pass
def eat(...):
pass
def __init__(self, name="pepe", type="warrior"):
self.name = name
self.type = type
self.health = 100
您的最后一个Personaje
类现在应该如下所示:
<ipython-input-22-9789ced9c556> in <module>()
----> 1 p.eat('leche')
AttributeError: Personaje instance has no attribute 'eat'
class Personaje:
def __init__(...):
pass
def eat(...):
pass
def __init__(self, name="pepe", type="warrior"):
self.name = name
self.type = type
self.health = 100
这是一个名错误吗?什么是
pepe
?您缺少一个空格:def\uu init\uuuuuu(self,name):
应该是def\uu init\uuuuuu(self,name):
非常感谢您,我非常感谢!来真的这是一个名错误吗?什么是pepe
?您缺少一个空格:def\uu init\uuuuuu(self,name):
应该是def\uu init\uuuuuu(self,name):
非常感谢您,我非常感谢!来真的我怎么会错过那张脸,类方法defeat
的缩进也关闭了。而且pepe
和warrior
都应该抛出namererror
s(我想,除非它们是全局定义的。至少有可能在发布代码之后,Personaje.\uuuu init\uuuu
调用eat
:)是的,我假设这个类只是整个python文件的一部分。@Victor Castillo Torres我想不清楚pepe和warrior是否应该是文件中其他地方定义的默认值或变量。我怎么会错过这个FacePalm,类方法def eat
的缩进也关闭了。而且pepe
和warrior
应该抛出NameError
s(除非它们是全局定义的,我想。至少有可能在发布代码之后,Personaje.\uuuu init\uuuu
调用eat
:)是的,我假设这个类只是整个python文件的一部分。@Victor Castillo Torres我想不清楚pepe和warrior是否应该是文件中其他地方定义的默认值或变量。