what is wrong?

N

neutrinman

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_name)
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(self):
self.hunger += 1
self.boredom += 1

def __feed_time(self): # 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_time()

def eat(self, food = 4):
print "Brruppp. Thank you."
self.hunger -= food
self.__feed_time() # I wrote this
print self.feed # I wrote this
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 choice
else:
print "\nSorry, but", choice, "isn't a valid choice."

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

Dennis Lee Bieber

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_name)
File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
self.feed = feed # I wrote this
AttributeError: can't set attribute
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)...

--
 
C

Christos TZOTZIOY Georgiou

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_name)
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?
[snip]

class Critter(object):

This fails as you said:
self.feed = feed # I wrote this

[snip]

And this would fail:
def __feed_time(self): # I worte this
self.feed += 1 # I wote this

Because of this:
feed = property(__get_feed) # I wrote this
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.
 
N

neutrinman

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

Mr. Bieber, it worked fine. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top