0

I'm very new to programming. I've been trying to learn Python via a book called "Python Programming for the Absolute Beginner". I'm working on classes. I've copied some code from one of the exercises in the book and get a Traceback (most recent call last): and NameError message. Below are the error messages I get along with the code. Please help. Thanks!

Traceback (most recent call last):
 File "C:\Python27\Ch8 Critter Caretaker Prg.py", line 5, in <module>
   class Critter(object):
 File "C:\Python27\Ch8 Critter Caretaker Prg.py", line 25, in Critter
   mood = property(__get_mood)
NameError: name '_Critter__get_mood' is not defined
# Critter Caretaker
# 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 __pass_time(self):
        self.hunger += 1
        self.bordedom += 1
    def __get_mode(self):
        unhappiness = self.hunger + self.boredom
        if unhappiness < 5:
            mood = "happy"
        elif 5 <= unhappiness <= 10:
            mood = "okay"
        elif 11 <= unhappiness <= 15:
            mood = "frustrated"
        else:
            mood = "mad"

    mood = property(__get_mood)

    def talk(self):
        print "I'm", self.name, "and I feel", self.mood, "now.\n"
        self.__pass_time()

    def eat(self, food = 4):
        print "Brrupp.  Thank you."
        self.hunger -= food
        if self.hunger < 0:
            self.hunger = 0
        self.__pass_time()

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

def main():
    crit_name = raw_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
        """

        choice = raw_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()

        # some unknown choie
        else:
            print "\nSorry, but", choice, "isn't a vaild choice."

main()
("\n\nPress the enter key to exit.")
Eric
  • 95,302
  • 53
  • 242
  • 374
user1816629
  • 219
  • 2
  • 5
  • 1
    If this book recommends the usage of leading double underscores then handle it with care. Is there any explanation why the code does not use only one underscore ? – Matthias Nov 11 '12 at 21:34

3 Answers3

2

As already mentioned, you made a typo between mode and mood. However, you'll hit more problems later - your __get_mood function never actually gets the mood, as it never returns. Also, you can use property as a decorator:

@property
def mood(self):
    unhappiness = self.hunger + self.boredom
    if unhappiness < 5:
        return "happy"
    elif 5 <= unhappiness <= 10:
        return "okay"
    elif 11 <= unhappiness <= 15:
        return "frustrated"
    else:
        return "mad"
Eric
  • 95,302
  • 53
  • 242
  • 374
1

It looks like you intended to define __get_mode(self) as __get_mood(self) instead.

To give some clarification about the NameError you are getting, the interpreter says that _Critter__get_mood is not defined rather than __get_mood is not defined because of name mangling, which is some Python fanciness to indicate private variables or methods in a class.

Community
  • 1
  • 1
kevintodisco
  • 5,061
  • 1
  • 22
  • 28
0

It's a simple typo. You define

def __get_mode(self):
#          ^^^

but access

_Critter__get_mood
#              ^^^
phihag
  • 278,196
  • 72
  • 453
  • 469