1

I made a simple program where animals make sounds. However, it prints none after each line in the shell. Is there something that should be change in the coding where the text is printed? or do I have to strip the text of anything?

Here is an example :

I am a Dog

None

Woof! Woof!

None

My code is:

import pets


def main():
    print "Choose number to hear what sound the animal makes."
    print "1. Enter choice"
    print "2. Dog"
    print "3. Cat"
    print "4. Bird"
    print "5. Quit program"
    choice = input("Choice: ")

    while choice != 5:

        if choice == 1:
            species = raw_input("Input an animal: ")
            userchoice = pets.Pet(species)
            print userchoice.show_species()
            print userchoice.make_sound()
            choice = input("Choice: ")

        elif choice == 2:
            userchoice = pets.Dog()
            print userchoice.show_species()
            print userchoice.make_sound()
            choice = input("Choice: ")

        elif choice == 3:
            userchoice = pets.Cat()
            print userchoice.show_species()
            print userchoice.make_sound()
            choice = input("Choice: ")

        elif choice == 4:
            userchoice = pets.Bird()
            print userchoice.show_species()
            print userchoice.make_sound()
            choice = input("Choice: ")

        else:
            print "Error"
            choice = input("Choice: ")


main()

the module imported is

class Pet:

    def __init__(self,species):
        self.__species = species

    def show_species(self):
        print "I am a ", self.__species

    def make_sound(self):
        print "I do not make a sound."


class Dog(Pet):

    def __init__(self):
        Pet.__init__(self, "Dog")

    def make_sound(self):
        print "Woof! Woof!"


class Bird(Pet):

    def __init__(self):
        Pet.__init__(self, "Bird")

    def make_sound(self):
        print "Chirp! Chirp!"


class Cat(Pet):

    def __init__(self):
        Pet.__init__(self, "Cat")

    def make_sound(self):
        print "Meow! Meow!"
  • 1
    Possible duplicate of [Python Script returns unintended "None" after execution of a function](http://stackoverflow.com/questions/16974901/python-script-returns-unintended-none-after-execution-of-a-function): See [Ashwini's answer](http://stackoverflow.com/questions/16974901/python-script-returns-unintended-none-after-execution-of-a-function/16974912#16974912) – LinkBerest May 15 '16 at 03:20

2 Answers2

1

You're printing the return value of make_sound here: print userchoice.make_sound(). Since make_sound doesn't explicitly specify return value None is returned by default and that's what you see in the output.

You can fix the problem by either not printing the return value or changing make_sound to return the sound instead of printing it.

niemmi
  • 17,113
  • 7
  • 35
  • 42
0

None is returned if nothing is explicitly returned from a function. Since you always print the return value but never return anything, you always print None.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358