Link to home
Start Free TrialLog in
Avatar of Dier Eluom
Dier Eluom

asked on

Python 3 script wont print

This python 3 script won't print.  Why?

class Fish:
    def __init__(self, first_name, last_name="Fish",
                 skeleton="bone", eyelids=False):
        self.first_name = first_name
        self.last_name = last_name
        self.skeleton = skeleton
        self.eyelids = eyelids

    def swim(self):
        print("The fish is swimming.")

    def swim_backwards(self):
        print("The fish can swim backwards.")
        
class Clownfish(Fish):
    
    #has own special method to live with anemone

    def live_with_anemone(self):
        print("The clownfish is coexisting with sea anemone.")
                # now create a Clownfish object

    casey = Clownfish("Casey")
    print(casey.first_name + " " + casey.last_name)
    casey.swim()
    casey.live_with_anemone()

Open in new window

Avatar of pepr
pepr

See the update below -- bad indentation.

The script only defines the classes. It does not use them. The code is not called.

To use it, you need to create the instance of the class, and then to call some of its methods, like this:
fish = Fish('Johny')
fish.swim()

Open in new window


I guess you are the beginner, but I do not know your age. So I do not know, whether the following comparison is good for you.

When compared with the real world, the class Fish is like the word fish in real language. You know that the world means something, and the something has some features. However, the word fish does not swim. You need the real entity that can swim, and that is associated with the word fish. The real entity equivalent in programming is called the instance of the class.

Update after looking closer


You have the code there at the lines 23+. However, the indentation is wrong. You must move the lines one level to the left. This way, Python thinks that the code belongs to the definition of the class Clownfish.
Instead of fixing the indentation, you can also write the following line above the line 23:
if __name__ == '__main__':

Open in new window


and then let the other lines indented as they are.
Avatar of Dier Eluom

ASKER

class Fish:
    def __init__(self, first_name, last_name="Fish",
                 skeleton="bone", eyelids=False):
        self.first_name = first_name
        self.last_name = last_name
        self.skeleton = skeleton
        self.eyelids = eyelids

    def swim(self):
        print("The fish is swimming.")

    def swim_backwards(self):
        print("The fish can swim backwards.")
       
class Clownfish(Fish):
   
    #has own special method to live with anemone

    def live_with_anemone(self):
        print("The clownfish is coexisting with sea anemone.")
                # now create a Clownfish object

if__trout__=='__main__'
   
casey = Clownfish("Casey")
print(casey.first_name + " " + casey.last_name)
casey.swim()
casey.live_with_anemone()

#still wont print?
The __name__ is the special identifier ;) Try this:
class Fish:
    def __init__(self, first_name, last_name="Fish",
                 skeleton="bone", eyelids=False):
        self.first_name = first_name
        self.last_name = last_name
        self.skeleton = skeleton
        self.eyelids = eyelids

    def swim(self):
        print("The fish is swimming.")

    def swim_backwards(self):
        print("The fish can swim backwards.")

class Clownfish(Fish):
    """has own special method to live with anemone"""
    def live_with_anemone(self):
        print("The clownfish is coexisting with sea anemone.")

if __name__ == '__main__':
    # now create a Clownfish object
    casey = Clownfish("Casey")
    print(casey.first_name + " " + casey.last_name)
    casey.swim()
    casey.live_with_anemone()

Open in new window

It should print:
d:\__Python\ee29118157>py a.py
Casey Fish
The fish is swimming.
The clownfish is coexisting with sea anemone.

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.