我昨天才开始学习课程并尝试将其纳入课程。经过多次尝试后,我终于得到了最后一行,打印出一些可行的东西,但它实际上并没有说出正确的事情。它说随机数。请注意,这是python。
以下是该计划:
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 against grass'
和'weak against water'
,但事实并非如此。
答案 0 :(得分:2)
我认为您对类中的代码如何工作感到困惑。
self.name = name
self.poketype = poketype
此代码表示您将能够访问该类实例的name
和poketype
。例如,这就是我所说的" access":
>>> a = Pokemon('charmander', 'fire')
>>> a.name
'charmander'
>>> a.poketype
'fire'
因此当你def
函数需要多个参数(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")
这不是你想要的。你想要的是不需要传递任何争论,它只会吐出一个输出。所以,你只需要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
答案 1 :(得分:0)
您必须在poketype
函数
weakness
参数
答案 2 :(得分:0)
你需要
If (self.poketype == 'fire')
它不知道你传递它的方式是什么类型。
答案 3 :(得分:0)
这是工人阶级。你有几个错误:
self.poketype
方法中使用poketype
而不是weakness
。Carmander
而不是Charmander
的最后一行中有一个假定的拼写错误(没什么大不了的)。Charmander
的{{1}}方法,因为你遗漏了括号。如果没有括号,将返回实际的weakness
函数。Pokemon.weakness
。这只是一种很好的做法。object
答案 4 :(得分:0)
根据您的print
语句语法,我假设您使用的是Python 2.x.
根据您当前对函数的定义,在函数内调用print语句。因此,只需将最后一行更改为:
Charmander.weakness('fire')
返回输出:
strong against grass
weak against water
原因是您的weakness
方法将poketype
作为参数。或者,您可以将weakness
方法定义为def weakness(self):
,然后将其称为Charmander.weakness()
。