简单程序出错,打印错误(python)
我昨天才开始学习课程,并试图将其纳入一个计划。经过多次尝试,我终于得到了最后一行,它打印了一些要工作的东西,但实际上并没有说正确的东西。上面写的是随机数。请注意,这是python 节目如下:简单程序出错,打印错误(python),python,Python,我昨天才开始学习课程,并试图将其纳入一个计划。经过多次尝试,我终于得到了最后一行,它打印了一些要工作的东西,但实际上并没有说正确的东西。上面写的是随机数。请注意,这是python 节目如下: class Pokemon: def __init__(self,name,poketype): self.name=name self.poketype=poketype def weakness(self,poketype): if poketype=='fire
class Pokemon:
def __init__(self,name,poketype):
self.name=name
self.poketype=poketype
def weakness(self,poketype):
if poketype=='fire':
print 'strong against grass'
print 'weak against water'
elif poketype=='water':
print 'strong against first'
print 'weak against grass'
elif poketype =='grass':
print 'strong against water'
print 'weak against fire'
Charmander=Pokemon('charmander','fire')
Squirtle=Pokemon('squirtle','water')
Bulbasaur=Pokemon('bulbasaur','grass')
print Carmander.weakness
它应该打印
'strong Again grass'
和'weak Again water'
,但实际上它不打印。您必须在弱点
函数中传递所需的poketype
参数
If (self.poketype == 'fire')
它不知道你是怎么通过它的。这是工人阶级。你犯了几个错误:
- 主要问题是您正在初始化但没有使用实例变量。实例变量的整个要点是避免在所有方法都密切相关的情况下,必须在方法之间传递无数个变量作为参数。要解决这个问题,您需要在整个
方法中使用
,而不仅仅是self.poketype
poketype
- 此外,在最后一行中,您编写了
,而不是Carmander
(没什么大不了的)Charmander
- 最后,您实际上并没有调用
的Charmander
方法,因为您省略了括号。如果没有括号,将返回实际的弱点
函数Pokemon.weakness
- 最后,不管您使用的是Python2.x还是Python3.x,都应该让类从
对象继承。这被认为是良好的做法
根据您的
print
语句语法,我假设您使用的是Python2.x
根据您当前对函数的定义,将在函数中调用打印语句。因此,只需将最后一行更改为:
Charmander.weakness('fire')
它返回输出:
strong against grass
weak against water
原因是您的
弱点
方法将poketype
作为参数。或者,您可以将弱点
方法定义为定义弱点(self):
,然后将其称为Charmander.弱点()
self.name = name
self.poketype = poketype
此代码意味着您将能够访问类实例的name
和poketype
。例如,这就是我所说的“访问”:
因此,当您定义一个需要多个参数(self
)的函数时,实际上意味着该函数接受更多的输入。换言之,此代码:
def weakness(self, poketype):
if poketype == 'fire':
print 'strong against grass'
print 'weak against water'
elif poketype == 'water':
print 'strong against first'
print 'weak against grass'
elif poketype == 'grass':
print 'strong against water'
print 'weak against fire'
将按以下方式运行:
>>> a = Pokemon('charmander', 'fire')
>>> a.weakness("passing in the poketype argument here")
这不是你想要的。您想要的是不需要传入arguemnt,它只会输出一个输出。因此,您只需要self
参数
def weakness(self):
if poketype == 'fire':
print 'strong against grass'
print 'weak against water'
elif poketype == 'water':
print 'strong against first'
print 'weak against grass'
elif poketype == 'grass':
print 'strong against water'
print 'weak against fire'
现在你会问:那么我如何获得poketype
?答案很简单:您只需像在类外一样访问它,除了使用self
而不是变量名
def weakness(self):
if self.poketype == 'fire':
print 'strong against grass'
print 'weak against water'
elif self.poketype == 'water':
print 'strong against first'
print 'weak against grass'
elif self.poketype == 'grass':
print 'strong against water'
print 'weak against fire'
就这样!你完了!最终代码:
class Pokemon:
def __init__(self, name, poketype):
self.name = name
self.poketype = poketype
def weakness(self):
if self.poketype == 'fire':
print 'strong against grass'
print 'weak against water'
elif self.poketype == 'water':
print 'strong against first'
print 'weak against grass'
elif self.poketype == 'grass':
print 'strong against water'
print 'weak against fire'
Charmander = Pokemon('charmander','fire')
Squirtle = Pokemon('squirtle','water')
Bulbasaur = Pokemon('bulbasaur','grass')
# Note the empty parentheses, if you miss them out, you are just referring to
# the function but not calling it!
>>> Charmander.weakness()
strong against grass
weak against water
正确格式的代码后面的4行也是其中的一部分……”我终于得到了最后一行,它打印了一些要工作的东西,但实际上它没有说正确的东西。它说的是随机数。“请添加错误文本,而不仅仅是这些模糊的报告。假设你纠正了错误,Charmander.devity
将是绑定方法,并打印出类似于
的内容。我假设您引用的“随机数”是口袋妖怪实例的内存地址,我想您是想调用该方法,比如Charmander.devity()
不,不应该有poketype
参数,和self.poketype
应该被引用。在这种情况下,请从函数中删除poketype
。试试看。请包括解释。谢谢。该代码运行良好。我现在要把它扩展到一个游戏中,谢谢你对我的错误所做的解释和一步一步的指导。我最近开始上课,显然没有学到一些“概念”。。。
def weakness(self):
if self.poketype == 'fire':
print 'strong against grass'
print 'weak against water'
elif self.poketype == 'water':
print 'strong against first'
print 'weak against grass'
elif self.poketype == 'grass':
print 'strong against water'
print 'weak against fire'
class Pokemon:
def __init__(self, name, poketype):
self.name = name
self.poketype = poketype
def weakness(self):
if self.poketype == 'fire':
print 'strong against grass'
print 'weak against water'
elif self.poketype == 'water':
print 'strong against first'
print 'weak against grass'
elif self.poketype == 'grass':
print 'strong against water'
print 'weak against fire'
Charmander = Pokemon('charmander','fire')
Squirtle = Pokemon('squirtle','water')
Bulbasaur = Pokemon('bulbasaur','grass')
# Note the empty parentheses, if you miss them out, you are just referring to
# the function but not calling it!
>>> Charmander.weakness()
strong against grass
weak against water