A Virtual Cat Example

From Knowledge Kitchen
Jump to navigation Jump to search


A virtual cat that does whatever it wants. Written in Python.

A few things to note:


The Cat class

Save this code into a file named cat.py. You will not execute (i.e. "run") this code. Rather, this code will be used by other test scripts which will be executed.

"""
This file contains the Cat class, which is like the mold from which Cats are made.
You can make individual Cat objects from this Cat class.
"""

#import the random module, this will give us access to some random number generating functions
import random


#define the Cat class from which Cat objects will be made
class Cat:
    """The plan (or mold) from which cats are made"""

    def __init__(self, name, age):
        """Inital setup of any cat objects"""
        print("Making a new cat named {} age {}...".format(name, age))
        self.name = name
        self.age = age
        self.wake_up() #our cats wake up once they are born

    def sleep(self):
        """Tell the cat to sleep"""
        if self.sleeping == False:
            self.say("Zzzzzz....")
        else:
            #if the cat is already asleep, it doesn't need to be told to sleep
            self.say("I'm already asleep... leave me alone.")
        self.sleeping = True

    def wake_up(self):
        """Tell the cat to wake up"""
        self.say("I'm awake!")
        self.sleeping = False

    def meow(self):
        """Tell the cat to meow, if it's awake"""
        if self.sleeping == False:
            self.say("Meow!")
        else:
            #if the cat is sleeping, it will purr instead of meowing
            self.say("I'm sleeping so I'll purr... Prrrrrrr")

    def hiss(self):
        """Tell the cat to hiss"""
        if self.sleeping == False:
            self.say("Hissssss")
        else:
            #if the cat is sleeping, it won't hiss!
            self.say("Why would I hiss in my sleep?!")

    def eat(self):
        """Tell the cat to eat"""
        if self.sleeping == False:
            self.say("Yum, this is tasty mush!")
        else:
            #if the cat is sleeping, it will eat in its sleep
            self.say("Yum, eating in my sleep is the best!")

    def say(self, msg):
        """Tell the cat to say something

        Keyword Arguments:
            msg -- the desired message the cat should say, as a string       
        """
        #include the cat's name in the message, so we know which cat is speaking
        print("{} utter, '{}'".format(self.name, msg))

    def do_something(self):
        """Generates a random number which determines which function to call"""
        #generate a random number between 1 and 5 inclusive... note how we use one of the random module's functions
        rand1 = random.randint(1,5)

        #instruct the cat to do different things, depending upon which number was generated
        #note how we use the if/elif statements to make a decision on what to do next
        if rand1 == 1:
            self.eat()
        elif rand1 == 2:
            self.sleep()
        elif rand1 == 3:
            self.wake_up()
        elif rand1 == 4:
            self.hiss()
        elif rand1 == 5:
            self.meow()

Test program

This script you can use to "test" the Cat class by creating some cat objects and instructing them to do something. This is the scripts you will actually want to execute.

Make two cats

Save this code into a file named make_two_cats.py:

"""This script creates two Cat objects from the Cat class and instructs each cat to perform an action of each Cats choosing"""

#load all the code from the cat.py file 
from cat import *

#make a cat (i.e. instantiate a Cat object from the Cat class)
cat1 = Cat("Fluffy", 10)

#instruct the cat to do something twice... the cat will randomly determine what to do
cat1.do_something()
cat1.do_something()

#make another cat (i.e. instantiate a Cat object from the Cat class)
cat2 = Cat("Sherry", 6)

#instruct the cat to do something twice... the cat will randomly determine what to do
cat2.do_something()
cat2.do_something()


What links here