{ "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", "Simple Critter\n", "Demonstrates creating a new type\n", "'''\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", "Constructor Critter\n", "Demonstrates constructors\n", "'''\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", "Private Critter\n", "Demonstrates setting member access levels\n", "'''\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", "//Static Critter\n", "//Demonstrates static member variables and functions\n", "'''\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", "//Critter Caretaker\n", "//Simulates caring for a virtual pet\n", "Note : Book program has error, PassTime() is taking time integer as argument, but while calling it , no-function is \n", "passing that time so this would give error.\n", "'''\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();\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();\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();\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();\n", " elif choice == 3:\n", " crit.Play();\n", " else:\n", " print \"\\nSorry, but \" , choice , \" isn't a valid choice.\\n\";\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "PassTime() takes exactly 2 arguments (1 given)", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 47\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 48\u001b[0m \u001b[0mcrit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCritter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 49\u001b[1;33m \u001b[0mcrit\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTalk\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 50\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 51\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nCritter Caretaker\\n\\n\"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m\u001b[0m in \u001b[0;36mTalk\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 30\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"happy.\\n\"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPassTime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mEat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfood\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: PassTime() takes exactly 2 arguments (1 given)" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "I'm a critter and I feel \n", "happy.\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 8.6 page no : 280" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "Critter Caretaker\n", "Simulates caring for a virtual pet\n", "'''\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 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }