Chapter 8 : Classes: Critter Caretaker

example 8.1 page no : 256

In [1]:
class Critter:
    def __init__(self):
        self.m_Hunger = 0

    def Greet(self):
        print "Hi. I'm a critter. My hunger level is " , self.m_Hunger

crit1 = Critter()
crit2 = Critter()
crit1.m_Hunger = 9;
print "crit1's hunger level is " , crit1.m_Hunger
crit2.m_Hunger = 3;
print "crit2's hunger level is " , crit2.m_Hunger
crit1.Greet()
crit2.Greet()
crit1's hunger level is  9
crit2's hunger level is  3
Hi. I'm a critter. My hunger level is  9
Hi. I'm a critter. My hunger level is  3

example 8.2 page no: 261

In [2]:
class Critter:
    def __init__(self,h=0):
        print  "A new critter has been born!" 
        self.m_Hunger = h

    def Greet(self):
        print "Hi. I'm a critter. My hunger level is " , self.m_Hunger

crit = Critter(7);
crit.Greet();
A new critter has been born!
Hi. I'm a critter. My hunger level is  7

example 8.3 page no : 265

In [3]:
class Critter:
    def __init__(self,h=0):
        print  "A new critter has been born!" 
        self.m_Hunger = h

    def Greet(self):
        print "Hi. I'm a critter. My hunger level is " , self.m_Hunger

    def GetHunger(self):
        return self.m_Hunger;

    def SetHunger(self,hunger):
        if (hunger < 0):
            print "You can't set a critter's hunger to a negative number.";
        else:
            self.m_Hunger = hunger;


crit = Critter(5)
#print crit.m_Hunger; //illegal, m_Hunger is private!
print "Calling GetHunger(): " , crit.GetHunger()
print "Calling SetHunger() with -1.";
crit.SetHunger(-1);
print "Calling SetHunger() with 9.";
crit.SetHunger(9);
print "Calling GetHunger(): " , crit.GetHunger()
A new critter has been born!
Calling GetHunger():  5
Calling SetHunger() with -1.
You can't set a critter's hunger to a negative number.
Calling SetHunger() with 9.
Calling GetHunger():  9

example 8.4 page no: 270

In [4]:
class Critter:
    s_Total = 0
    def __init__(self,h=0):
        print  "A new critter has been born!" 
        self.m_Hunger = h
        Critter.s_Total += 1

    def GetTotal(self):
        return Critter.s_Total;

print "The total number of critters is: ";
print Critter.s_Total
crit1 = Critter() 
crit2 = Critter()
crit3 = Critter()
print "\nThe total number of critters is: ",
print Critter.GetTotal(crit1) 
The total number of critters is: 
0
A new critter has been born!
A new critter has been born!
A new critter has been born!

The total number of critters is:  3

example 8.5 page no : 277

In [4]:
class Critter:
    def __init__(self,hunger = 0, boredom = 0):
        self.m_Hunger= hunger
        self.m_Boredom = boredom

    def  GetMood(self):
        return (self.m_Hunger + self.m_Boredom);

    def PassTime(self,time):
        self.m_Hunger += time;
        self.m_Boredom += time;

    def Talk(self):
        print "I'm a critter and I feel ";
        mood = self.GetMood();
        if (mood > 15):
            print "mad.\n";
        elif (mood > 10):
            print "frustrated.\n";
        elif (mood > 5):
            print "okay.\n";
        else:
            print  "happy.\n";
        self.PassTime(0);

    def Eat(self,food):
        print  "Brruppp.\n";
        self.m_Hunger -= food;
        if (self.m_Hunger < 0):
            self.m_Hunger = 0;
        self.PassTime(0);

    def Play(self,fun):
        print "Wheee!\n";
        self.m_Boredom -= fun;
        if (self.m_Boredom < 0):
            self.m_Boredom = 0;
        self.PassTime(0);

crit = Critter()
crit.Talk();
while True:
    print "\nCritter Caretaker\n\n";
    print "0 - Quit\n";
    print "1 -  Listen to your critter\n";
    print "2 - Feed your critter\n";
    print "3 - Play with your critter\n\n";
    print  "Choice: ";
    choice = int(raw_input())
    if choice == 0:
        print  "Good-bye.\n";
        break
    elif choice == 1:
        crit.Talk();
    elif choice == 2:
        crit.Eat(0);
    elif choice == 3:
        crit.Play(0);
    else:
        print  "\nSorry, but " , choice , " isn't a valid choice.\n";
I'm a critter and I feel 
happy.


Critter Caretaker


0 - Quit

1 -  Listen to your critter

2 - Feed your critter

3 - Play with your critter


Choice: 
1
I'm a critter and I feel 
happy.


Critter Caretaker


0 - Quit

1 -  Listen to your critter

2 - Feed your critter

3 - Play with your critter


Choice: 
2
Brruppp.


Critter Caretaker


0 - Quit

1 -  Listen to your critter

2 - Feed your critter

3 - Play with your critter


Choice: 
3
Wheee!


Critter Caretaker


0 - Quit

1 -  Listen to your critter

2 - Feed your critter

3 - Play with your critter


Choice: 
0
Good-bye.

example 8.6 page no : 280

In [7]:
class Critter:
    def __init__(self,hunger = 0, boredom = 0):
        self.m_Hunger= hunger
        self.m_Boredom = boredom

    def Talk(self):
        print "I'm a critter and I feel ";
        mood = self.GetMood();
        if (mood > 15):
            print "mad.\n";
        elif (mood > 10):
            print "frustrated.\n";
        elif (mood > 5):
            print "okay.\n";
        else:
            print "happy.\n";
        self.PassTime();
        
    def Eat(self,food = 4):
        print "Brruppp.";
        self.m_Hunger -= food;
        if (self.m_Hunger < 0):
            self.m_Hunger = 0;
        self.PassTime();
        
    def Play(self,fun = 4):
        print "Wheee!";
        self.m_Boredom -= fun;
        if (self.m_Boredom < 0):
            self.m_Boredom = 0;
        self.PassTime();
    
    def PassTime(self, time = 1):
        self.m_Hunger += time;
        self.m_Boredom += time;


    def GetMood(self):
        return (self.m_Hunger + self.m_Boredom);

crit = Critter()
crit.Talk();
choice = 1;
choices = [1,2,3,0]
i = 0
while choice != 0 :
    print "\nCritter Caretaker\n";
    print "0 - Quit";
    print "1 - Listen to your critter";
    print "2 - Feed your critter";
    print "3 - Play with your critter\n";

    print "Choice: ",
    choice = choices[i] #int(raw_input())
    i += 1

    if choice ==0:
        print "Good-bye.\n";
    elif choice==1:
        crit.Talk();    
    elif choice == 2:
        crit.Eat();
    elif choice == 3:
        crit.Play();
    else:
        print "\nSorry, but " , choice , " isn't a valid choice.";
 I'm a critter and I feel 
happy.


Critter Caretaker

0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter

Choice:  I'm a critter and I feel 
happy.


Critter Caretaker

0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter

Choice:  Brruppp.

Critter Caretaker

0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter

Choice:  Wheee!

Critter Caretaker

0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter

Choice:  Good-bye.