一个简单的程序出错,打印错误(python)

时间:2016-06-10 04:21:27

标签: python

我昨天才开始学习课程并尝试将其纳入课程。经过多次尝试后,我终于得到了最后一行,打印出一些可行的东西,但它实际上并没有说出正确的事情。它说随机数。请注意,这是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',但事实并非如此。

5 个答案:

答案 0 :(得分:2)

我认为您对类中的代码如何工作感到困惑。

self.name = name
self.poketype = poketype

此代码表示您将能够访问该类实例的namepoketype。例如,这就是我所说的" 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函数。
  • 最后,无论您使用的是Python 2.x还是Python 3.x,都应该让您的类继承自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()