{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 8 : Classes: Critter Caretaker" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.1 page no : 256" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Critter:\n", " def __init__(self):\n", " self.m_Hunger = 0\n", "\n", " def Greet(self):\n", " print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n", "\n", "crit1 = Critter()\n", "crit2 = Critter()\n", "crit1.m_Hunger = 9;\n", "print \"crit1's hunger level is \" , crit1.m_Hunger\n", "crit2.m_Hunger = 3;\n", "print \"crit2's hunger level is \" , crit2.m_Hunger\n", "crit1.Greet()\n", "crit2.Greet()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "crit1's hunger level is 9\n", "crit2's hunger level is 3\n", "Hi. I'm a critter. My hunger level is 9\n", "Hi. I'm a critter. My hunger level is 3\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.2 page no: 261" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Critter:\n", " def __init__(self,h=0):\n", " print \"A new critter has been born!\" \n", " self.m_Hunger = h\n", "\n", " def Greet(self):\n", " print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n", "\n", "crit = Critter(7);\n", "crit.Greet();" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A new critter has been born!\n", "Hi. I'm a critter. My hunger level is 7\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.3 page no : 265" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Critter:\n", " def __init__(self,h=0):\n", " print \"A new critter has been born!\" \n", " self.m_Hunger = h\n", "\n", " def Greet(self):\n", " print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n", "\n", " def GetHunger(self):\n", " return self.m_Hunger;\n", "\n", " def SetHunger(self,hunger):\n", " if (hunger < 0):\n", " print \"You can't set a critter's hunger to a negative number.\";\n", " else:\n", " self.m_Hunger = hunger;\n", "\n", "\n", "crit = Critter(5)\n", "#print crit.m_Hunger; //illegal, m_Hunger is private!\n", "print \"Calling GetHunger(): \" , crit.GetHunger()\n", "print \"Calling SetHunger() with -1.\";\n", "crit.SetHunger(-1);\n", "print \"Calling SetHunger() with 9.\";\n", "crit.SetHunger(9);\n", "print \"Calling GetHunger(): \" , crit.GetHunger()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A new critter has been born!\n", "Calling GetHunger(): 5\n", "Calling SetHunger() with -1.\n", "You can't set a critter's hunger to a negative number.\n", "Calling SetHunger() with 9.\n", "Calling GetHunger(): 9\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.4 page no: 270" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Critter:\n", " s_Total = 0\n", " def __init__(self,h=0):\n", " print \"A new critter has been born!\" \n", " self.m_Hunger = h\n", " Critter.s_Total += 1\n", "\n", " def GetTotal(self):\n", " return Critter.s_Total;\n", "\n", "print \"The total number of critters is: \";\n", "print Critter.s_Total\n", "crit1 = Critter() \n", "crit2 = Critter()\n", "crit3 = Critter()\n", "print \"\\nThe total number of critters is: \",\n", "print Critter.GetTotal(crit1) " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The total number of critters is: \n", "0\n", "A new critter has been born!\n", "A new critter has been born!\n", "A new critter has been born!\n", "\n", "The total number of critters is: 3\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.5 page no : 277" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "class Critter:\n", " def __init__(self,hunger = 0, boredom = 0):\n", " self.m_Hunger= hunger\n", " self.m_Boredom = boredom\n", "\n", " def GetMood(self):\n", " return (self.m_Hunger + self.m_Boredom);\n", "\n", " def PassTime(self,time):\n", " self.m_Hunger += time;\n", " self.m_Boredom += time;\n", "\n", " def Talk(self):\n", " print \"I'm a critter and I feel \";\n", " mood = self.GetMood();\n", " if (mood > 15):\n", " print \"mad.\\n\";\n", " elif (mood > 10):\n", " print \"frustrated.\\n\";\n", " elif (mood > 5):\n", " print \"okay.\\n\";\n", " else:\n", " print \"happy.\\n\";\n", " self.PassTime(0);\n", "\n", " def Eat(self,food):\n", " print \"Brruppp.\\n\";\n", " self.m_Hunger -= food;\n", " if (self.m_Hunger < 0):\n", " self.m_Hunger = 0;\n", " self.PassTime(0);\n", "\n", " def Play(self,fun):\n", " print \"Wheee!\\n\";\n", " self.m_Boredom -= fun;\n", " if (self.m_Boredom < 0):\n", " self.m_Boredom = 0;\n", " self.PassTime(0);\n", "\n", "crit = Critter()\n", "crit.Talk();\n", "while True:\n", " print \"\\nCritter Caretaker\\n\\n\";\n", " print \"0 - Quit\\n\";\n", " print \"1 - Listen to your critter\\n\";\n", " print \"2 - Feed your critter\\n\";\n", " print \"3 - Play with your critter\\n\\n\";\n", " print \"Choice: \";\n", " choice = int(raw_input())\n", " if choice == 0:\n", " print \"Good-bye.\\n\";\n", " break\n", " elif choice == 1:\n", " crit.Talk();\n", " elif choice == 2:\n", " crit.Eat(0);\n", " elif choice == 3:\n", " crit.Play(0);\n", " else:\n", " print \"\\nSorry, but \" , choice , \" isn't a valid choice.\\n\";\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I'm a critter and I feel \n", "happy.\n", "\n", "\n", "Critter Caretaker\n", "\n", "\n", "0 - Quit\n", "\n", "1 - Listen to your critter\n", "\n", "2 - Feed your critter\n", "\n", "3 - Play with your critter\n", "\n", "\n", "Choice: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "I'm a critter and I feel \n", "happy.\n", "\n", "\n", "Critter Caretaker\n", "\n", "\n", "0 - Quit\n", "\n", "1 - Listen to your critter\n", "\n", "2 - Feed your critter\n", "\n", "3 - Play with your critter\n", "\n", "\n", "Choice: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Brruppp.\n", "\n", "\n", "Critter Caretaker\n", "\n", "\n", "0 - Quit\n", "\n", "1 - Listen to your critter\n", "\n", "2 - Feed your critter\n", "\n", "3 - Play with your critter\n", "\n", "\n", "Choice: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Wheee!\n", "\n", "\n", "Critter Caretaker\n", "\n", "\n", "0 - Quit\n", "\n", "1 - Listen to your critter\n", "\n", "2 - Feed your critter\n", "\n", "3 - Play with your critter\n", "\n", "\n", "Choice: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Good-bye.\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.6 page no : 280" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "class Critter:\n", " def __init__(self,hunger = 0, boredom = 0):\n", " self.m_Hunger= hunger\n", " self.m_Boredom = boredom\n", "\n", " def Talk(self):\n", " print \"I'm a critter and I feel \";\n", " mood = self.GetMood();\n", " if (mood > 15):\n", " print \"mad.\\n\";\n", " elif (mood > 10):\n", " print \"frustrated.\\n\";\n", " elif (mood > 5):\n", " print \"okay.\\n\";\n", " else:\n", " print \"happy.\\n\";\n", " self.PassTime();\n", " \n", " def Eat(self,food = 4):\n", " print \"Brruppp.\";\n", " self.m_Hunger -= food;\n", " if (self.m_Hunger < 0):\n", " self.m_Hunger = 0;\n", " self.PassTime();\n", " \n", " def Play(self,fun = 4):\n", " print \"Wheee!\";\n", " self.m_Boredom -= fun;\n", " if (self.m_Boredom < 0):\n", " self.m_Boredom = 0;\n", " self.PassTime();\n", " \n", " def PassTime(self, time = 1):\n", " self.m_Hunger += time;\n", " self.m_Boredom += time;\n", "\n", "\n", " def GetMood(self):\n", " return (self.m_Hunger + self.m_Boredom);\n", "\n", "crit = Critter()\n", "crit.Talk();\n", "choice = 1;\n", "choices = [1,2,3,0]\n", "i = 0\n", "while choice != 0 :\n", " 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", "\n", " print \"Choice: \",\n", " choice = choices[i] #int(raw_input())\n", " i += 1\n", "\n", " if choice ==0:\n", " print \"Good-bye.\\n\";\n", " elif choice==1:\n", " crit.Talk(); \n", " elif choice == 2:\n", " crit.Eat();\n", " elif choice == 3:\n", " crit.Play();\n", " else:\n", " print \"\\nSorry, but \" , choice , \" isn't a valid choice.\";" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " I'm a critter and I feel \n", "happy.\n", "\n", "\n", "Critter Caretaker\n", "\n", "0 - Quit\n", "1 - Listen to your critter\n", "2 - Feed your critter\n", "3 - Play with your critter\n", "\n", "Choice: I'm a critter and I feel \n", "happy.\n", "\n", "\n", "Critter Caretaker\n", "\n", "0 - Quit\n", "1 - Listen to your critter\n", "2 - Feed your critter\n", "3 - Play with your critter\n", "\n", "Choice: Brruppp.\n", "\n", "Critter Caretaker\n", "\n", "0 - Quit\n", "1 - Listen to your critter\n", "2 - Feed your critter\n", "3 - Play with your critter\n", "\n", "Choice: Wheee!\n", "\n", "Critter Caretaker\n", "\n", "0 - Quit\n", "1 - Listen to your critter\n", "2 - Feed your critter\n", "3 - Play with your critter\n", "\n", "Choice: Good-bye.\n", "\n" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }