Remain Remain - 5 months ago 10
Python Question

Error with a simple program, printing error (python)

I've only yesterday began learning about classes and tried to incorporate it into a program. After numerous tries, I finally got the last line, which prints somethings to work, but it doesn't actually say the correct thing. It says random numbers. Note that this is python.

Here is the program:

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


That is supposed to print
'strong against grass'
and
'weak against water'
, but it just doesn't.

Answer

I think you are confused about how code in classes work.

self.name = name
self.poketype = poketype

This code means that you will be able to access name and poketype of an instance of the class. For example, this is what I mean by "access":

>>> a = Pokemon('charmander', 'fire')
>>> a.name
'charmander'
>>> a.poketype
'fire' 

So when you define a function that needs more than one argument (self), it actually means that thte function takes in more inputs. In other words this code:

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'

Will be run as:

>>> a = Pokemon('charmander', 'fire')
>>> a.weakness("passing in the poketype argument here")

Which is not what you want. What you want is that no arguemnt needs to be passed in and it will just spit out an output. So, you just need the self argument.

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'

Now you would ask: How do I get the poketype then? The answer is simple: You just access it like you would outside of the class, except, you use self instead of the variable name.

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'

And that's it! You're done! The final code:

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
Comments