0

Why do I get None after each result of the print statement?

I have never come across this while using the print statement or class instantiation...

Where is None coming from?

class Enemy:

    life = 3

    def attack(self):
        print("ouch!")
        self.life -= 1

    def checkLife(self):
        if (self.life <= 0):
            print('dead!')
        else:
            print( str(self.life) + " lives left..." )


e1 = Enemy()

print( e1.checkLife() )
# 3 lives left...
# None

print( e1.attack() )
# ouch!
# None

print( e1.checkLife() )
# 2 lives left...
# None
  • 1
    https://stackoverflow.com/questions/7664779/what-is-the-formal-difference-between-print-and-return – Georgy Jan 27 '18 at 15:35

5 Answers5

1
class Enemy:

    life = 3

def attack(self):
    print("ouch!")
    self.life -= 1

def checkLife(self):
    if (self.life <= 0):
        print('dead!')
    else:
        print( str(self.life) + " lives left..." )


e1 = Enemy()

e1.checkLife()


e1.attack()


e1.checkLife()

you dont need the print infront of the function calls, as there is a print function inside every function, it will print what you want to see, the function itself returns None as there is nothing to return, thus printing it gives you None

Exprator
  • 26,992
  • 6
  • 47
  • 59
1

You're printing the return value of functions that don't return a value.

print( e1.checkLife() )
# 3 lives left...        This is printed from within the function
# None        checkLife() returns None, and you print it here
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

All Python functions return something—None if you use just return (or no return at all). Don’t print it if you don’t want to see it. In an interactive session, the interpreter automatically prints the value of each expression unless it is None.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
1

When you define a class, every method of your class (functions of your class) should have correct identation. In your code attack, checkLife are interpreted like independent functions. That's the reason when you try to call checkLife you get:

AttributeError: Enemy instance has no attribute 'checkLife'

Also you must define a constructor for your class, a special kind of function which is responsible to create your object properly. In your case your enemy's life should be initialized to 3. You should define this attribute in your constructor, so your object can have it. That's the reason self.life works.

self.life refers to your object's (self) attribute with the name life.

class Enemy:
    def __init__(self):
        self.life = 3

    def attack(self):
        print("ouch!")
        self.life -= 1

    def checkLife(self):
        if (self.life <= 0):
            print('dead!')
        else:
            print( str(self.life) + " lives left..." )


e1 = Enemy()

print( e1.checkLife() )
# 3 lives left...
# None

print( e1.attack() )
# ouch!
# None

print( e1.checkLife() )
# 2 lives left...
# None
Nikolas
  • 161
  • 1
  • 12
  • Sorry, my bad. This happened while I was copying my code and pasting it into this post. I will edit it now... –  Jan 27 '18 at 15:46
0

All Python functions return the value None unless there is an explicit return statement with a value other than None. Instead, the value None will be returned.

So the second print is actually returning the results of the functions which are both None. The first print inside the function is just enough. Or you update you function to have explicit return and then print the returned values as follow:

class Enemy:

    life = 3

    def attack(self):
       print("ouch!")
       self.life -= 1
       return self.life

    def checkLife(self):
        if (self.life <= 0):
            return 'dead!'
        else:
            return str(self.life) + " lives left..."


e1 = Enemy()

print( e1.checkLife() )
# 3 lives left...

print( e1.attack() )
# ouch!
# 2

print( e1.checkLife() )
# 2 lives left...
Dhia
  • 10,119
  • 11
  • 58
  • 69