<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">from zoology import Animal, Fish, Parrot, Bird, Penguin
"""
A module that supports creating and admitting animals to the zoo.
Users work with a menu that allows them to select from a variety of
actions and queries surrounding the zoo residents.

You can run this code by typing 'python zoo.py'
at the command prompt

Author: Bracy (awb93), Lee (ljl2), and Fan (kdf4)
Date:   April 2020
"""

class Zoo():
    """
    A class representing a zoo, a collection of animals.

    INSTANCE ATTRIBUTES:
        name: the name of the zoo [str]
        animals: list of animals, the tag number of an animal serves as the
            index of that animal in the list

    An instance is a zoo with a name and no animals.
    """
    # BUILT-IN METHODS
    def __init__(self, name):
        """
        Creates a new Zoo with no enclosures and no animals
        """
        # Instance attributes
        self.name = name
        self.animals = []

    def listAnimals(self):
        """
        Prints out each animal in the zoo.
        """
        print("Here are the animals we've got:")
        for a in self.animals:
            print(a)

    def listenToAnimals(self):
        """
        Asks each animal in the zoo to speak.
        """
        print("We'll ask all the anmials to talk")
        for a in self.animals:
            print("hey "+a.name+", say something:")
            a.speak("Hi my name is "+a.name)

    def addAnimal(self):
        """
        Gets enough information from the user to create a new
        animal and add it to the zoo.
        """
        type = input("What type of animal would you like to add? ")
        name = input("What's the animal's name? ")
        new_animal = None

        if type == "Animal":
            new_animal = Animal(name)
        elif type == "Fish":
            new_animal = Fish(name)
        elif type == "Parrot":
            new_animal = Parrot(name)
        elif type == "Bird":
            new_animal = Bird(name)
        elif type == "Penguin":
            new_animal = Penguin(name)

        if new_animal == None:
            print("We can't add that type of animal")
        else:
            self.admit(new_animal)

    def populate(self):
        """
        Creates and adds a handful of animals to the zoo.
        """
        a = Fish("Nemo")
        self.admit(a)
        a = Animal("Lassie")
        self.admit(a)
        a = Parrot("Polly")
        self.admit(a)
        a = Parrot("Alex")
        self.admit(a)
        a = Penguin("Chilly Willy")
        self.admit(a)
        a = Penguin("Pingu")
        self.admit(a)

    def admit(self, animal):
        """
        Given an animal, adds it to the zoo.

        animal: an object of type Animal
        """

        assert(isinstance(animal, Animal))
        assert(animal.tag_no==len(self.animals))

        # put them on the animal list
        self.animals.append(animal)
        print("There are now "+str(len(self.animals))+" animals in "+self.name+".")

    def get_animal(self):
        """ A helper function that asks the user for a tag number
        and then finds that animal in the zoo

        Returns an object of type Animal or None if user specifies
        a non-existant tag number.
        """
        tag_no = int(input("what's the tag number of the animal in question? "))
        animal = None
        if tag_no &lt; len(self.animals):
            animal = self.animals[tag_no]
        else:
            print("there is no animal with that tag number!")
        return animal

    def lookup(self):
        """
        User specifies the animal they're interested in.
        This function then prints out the information about the animal.
        """
        animal = self.get_animal()
        if animal != None:
            print(animal)

    def feed(self):
        """
        User specifies the animal they're interested in.
        This function then feeds the animal.
        """
        animal = self.get_animal()
        if animal != None:
            animal.eat()

    def print_menu(self):
        print("\nWelcome to "+self.name)
        print("\tWould you me like to:")
        print("\ta - add an animal to the zoo")
        print("\tb - list all the animals in the zoo")
        print("\tc - listen to all the animals in the zoo")
        print("\td - look up a specific animal in the zoo")
        print("\te - feed an animal in the zoo")
        print("\tf - pre-load a few animals")
        print("\tq - quit")

    def interact(self):
        """
        Simple loop that offers a menu of choices to a user
        so they can interact with the zoo.
        """
        selection = ""
        while selection != 'q':
            if selection == 'a':
                self.addAnimal()
            elif selection == 'b':
                self.listAnimals()
            elif selection == 'c':
                self.listenToAnimals()
            elif selection == 'd':
                self.lookup()
            elif selection == 'e':
                self.feed()
            elif selection == 'f':
                self.populate()
            self.print_menu()
            selection = input("Your selection: ")

the_zoo = Zoo("the Hogle Zoo")

the_zoo.interact()
</pre></body></html>