The Critter Caretaker Program

This program is from Chapter 8: Python Programming for the Absolute Beginner, Third Edition - Michael Dawson. It has some minor amendments.

It extends the previous critter program to allow us to interact with the critter. The idea is that the critter can get hungry and bored which makes them frustrated or mad.

We can then feed or play with our critter to improve its hunger and boredom to improve its mood.

Our new Critter class adds instance attributes hunger and boredom to each critter, this allows them to have state. We can then manage and interact with this state through our program.

Create a file called critter.py and copy in the following:

#critter.py
# A virtual pet to care for
class Critter(object):
  """A virtual pet"""

  def __init__(self, name, hunger=0, boredom=0):
    self.name = name
    self.hunger = hunger
    self.boredom = boredom

  def __str__(self):
    desc = ""
    desc += f"Name: {self.name}\n"
    desc += f"Hunger: {self.hunger}\n"
    desc += f"Boredom: {self.boredom}\n"
    return desc

  # indicate that the method should only be used internally
  # using a single leading underscore
  def _pass_time(self):
    self.hunger += 1
    self.boredom += 1

  # indicate that the method should only be used internally
  # using a single leading underscore
  def _mood(self):
    unhappiness = self.hunger + self.boredom
    if unhappiness < 5:
      m = "happy"
    elif 5 <= unhappiness <= 10:
      m = "okay"
    elif 11 <= unhappiness <= 15:
      m = "frustrated"
    else:
      m = "mad"
    return m

  def talk(self):
    print(f"I'm {self.name} and I feel {self._mood()} now.\n")
    self._pass_time()

  def eat(self, food=5):
    print("Brruppp. Thank you.")
    self.hunger -= food
    if self.hunger < 0:
      self.hunger = 0
    self._pass_time()

  def play(self, fun=5):
    print("Wheee!")
    self.boredom -= fun
    if self.boredom < 0:
      self.boredom = 0
    self._pass_time()

You will see a bunch of instance methods that allow us to interact with our critter. You should spend some time trying to understand what this class is doing.

Our main.py file contains the following:

from critter import Critter

def main():
  crit_name = input("What do you want to name your critter?: ")
  crit = Critter(crit_name)

  choice = None
  while choice != "0":
    print \
    ("""
    Critter Caretaker
    0 - Quit
    1 - Listen to your critter
    2 - Feed your critter
    3 - Play with your critter
    4 - Print Critter Stats (does not pass time)
    """)
  
    choice = input("Choice: ")
    print()
    # exit
    if choice == "0":
      print("Good-bye.")
    # listen to your critter
    elif choice == "1":
      crit.talk()
    # feed your critter
    elif choice == "2":
      crit.eat()
    # play with your critter
    elif choice == "3":
      crit.play()
    # print critter stats
    elif choice == "4":
      print(crit)
    # some unknown choice
    else:
      print(f"\nSorry, but {choice} isn't a valid choice.")

if __name__ == "__main__":
  main()