what is wrong?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neutrinman@myrealbox.com

    #1

    what is wrong?

    I cannot find out why the following code generates the error:
    Traceback (most recent call last):
    File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ?
    main()
    File "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main
    crit = Critter(crit_na me)
    File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
    self.feed = feed # I wrote this
    AttributeError: can't set attribute

    I add some codes to a program on a book. The lines that have "I wrote
    this" comment is the added codes. Could anyone tell me what is worng
    here?

    --------------
    ##
    class Critter(object) :
    """A virtual pet"""
    def __init__(self, name, hunger = 0, boredom = 0, feed = 0):
    self.name = name
    self.hunger = hunger
    self.boredom = boredom
    self.feed = feed # I wrote this

    def __pass_time(sel f):
    self.hunger += 1
    self.boredom += 1

    def __feed_time(sel f): # I worte this
    self.feed += 1 # I wote this

    def __get_feed(self ): # I wrote this
    return self.feed # I worte this

    feed = property(__get_ feed) # I wrote this

    def __get_mood(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"
    return mood

    mood = property(__get_ mood)

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

    def eat(self, food = 4):
    print "Brruppp. Thank you."
    self.hunger -= food
    self.__feed_tim e() # I wrote this
    print self.feed # I wrote this
    if self.hunger < 0:
    self.hunger = 0
    self.__pass_tim e()

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

    ##

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

    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("Choi ce: ")
    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 choice
    else:
    print "\nSorry, but", choice, "isn't a valid choice."

    main()
    ("\n\nPress the enter key to exit.")

  • Dennis Lee Bieber

    #2
    Re: what is wrong?

    On 24 Feb 2005 08:34:09 -0800, neutrinman@myre albox.com declaimed the
    following in comp.lang.pytho n:
    [color=blue]
    > I cannot find out why the following code generates the error:
    > Traceback (most recent call last):
    > File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ?
    > main()
    > File "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main
    > crit = Critter(crit_na me)
    > File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
    > self.feed = feed # I wrote this
    > AttributeError: can't set attribute
    >[/color]
    Off-hand, you defined "feed" to be a property, and created a get
    method for it... but no set method.

    I could be all wet, since I haven't used properties in Python
    (haven't written anything big enough, meant for other users, to need the
    protection).

    The other problem is that it is ambiguous if

    self.feed

    refers to the data storage space for the value, or the PROPERTY itself

    feed = property().

    Try changing all the

    self.feed

    references to something like

    self.myFeed

    and report back if that works (or doesn't)...

    --[color=blue]
    > =============== =============== =============== =============== == <
    > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
    > wulfraed@dm.net | Bestiaria Support Staff <
    > =============== =============== =============== =============== == <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

    Comment

    • Christos TZOTZIOY Georgiou

      #3
      Re: what is wrong?

      On 24 Feb 2005 08:34:09 -0800, rumours say that neutrinman@myre albox.com might
      have written:
      [color=blue]
      >I cannot find out why the following code generates the error:
      >Traceback (most recent call last):
      > File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ?
      > main()
      > File "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main
      > crit = Critter(crit_na me)
      > File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
      > self.feed = feed # I wrote this
      >AttributeError : can't set attribute[/color]
      [color=blue]
      >I add some codes to a program on a book. The lines that have "I wrote
      >this" comment is the added codes. Could anyone tell me what is worng
      >here?[/color]

      [snip]
      [color=blue]
      >class Critter(object) :[/color]

      This fails as you said:[color=blue]
      > self.feed = feed # I wrote this[/color]

      [snip]

      And this would fail:[color=blue]
      > def __feed_time(sel f): # I worte this
      > self.feed += 1 # I wote this[/color]

      Because of this:[color=blue]
      > feed = property(__get_ feed) # I wrote this[/color]
      which specifies only a 'get' method for the property.

      [snip]

      feed is a property, and there is not 'set' method for it, so it's behaving as
      read-only. Search for 'property' in your python docs.
      --
      TZOTZIOY, I speak England very best.
      "Be strict when sending and tolerant when receiving." (from RFC1958)
      I really should keep that in mind when talking with people, actually...

      Comment

      • neutrinman@myrealbox.com

        #4
        Re: what is wrong?

        I appreciate all of your help.
        I learned a lot form your adovice.
        Thanks.

        Mr. Bieber, it worked fine. Thanks again.

        Comment

        Working...