<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Can someone show me how to code this correctly please<br>
<br>
> class Critter(object):<br>
><br>
> def __init__(self, name, hunger = 0, boredom = 0):<br>
> def __pass_time(self):<br>
> def __str__(self):<br>
> @property<br>
> def mood(self):<br>
> def talk(self):<br>
> def eat(self):<br>
> def play(self):<br>
><br>
> class Farm(Critter):<br>
<br>
I still don't think a Farm is a type of Critter...<br>
<br>
> def __init__(self,farmlet):<br>
> Critter.__init__(self,farmlet)<br>
<br>
This will set the name to farmlet, which I don't<br>
think you want.<br>
<br>
> def talk(self,farmlet):<br>
<br>
You don't need to pass farmlet in since the<br>
class has farmlet stored inside it.<br>
You can access farmlet with self.farmlet.<br>
<br>
> for critter in farmlet:<br>
> print("Hello")<br>
> Critter.talk(farmlet)<br>
<br>
You want the instance to talk not the class.<br>
So you need to use critter.talk() And the talk<br>
method does not take any arguments except<br>
self. What you are doing here is calling the<br>
class method with an instance value of farmlet.<br>
ie self in that method gets the value of farmlet.<br>
<br>
> def main():<br>
> crit1 = Critter("Sweetie")<br>
> crit2 = Critter("Dave")<br>
> farmlet = [crit1,crit2]<br>
> f = Farm(farmlet)<br>
><br>
> choice = None<br>
> while choice != "0":<br>
> print \<br>
> ("""<br>
> Critter Caretaker<br>
><br>
> 0 - Quit<br>
> 1 - Listen to your critter<br>
> 2 - Feed your critter<br>
> 3 - Play with your critter<br>
> """)<br>
><br>
> choice = input("Choice: ")<br>
> print()<br>
><br>
> # exit<br>
> if choice == "0":<br>
> print("Good-bye.")<br>
><br>
> # listen to your critter<br>
> elif choice == "1":<br>
> f.talk(farmlet)<br>
><br>
> # feed your critter<br>
> elif choice == "2":<br>
> f.eat(farmlet)<br>
<br>
Note that f.eat is a method you inherit from Critter.<br>
The Critter method does not take any arguments<br>
so this will fail.<br>
<br>
> # play with your critter<br>
> elif choice == "3":<br>
> f.play(farmlet)<br>
<br>
Same with f.play()<br>
<br>
> Traceback (most recent call last):<br>
> File "D:/David/Python/programs/critter_farm3.py", line 72, in talk<br>
> Critter.talk(farmlet)<br>
> File "D:/David/Python/programs/critter_farm3.py", line 38, in talk<br>
> print("I'm", <a href="http://self.name" target="_blank">self.name</a>, "and I feel", self.mood, "now.\n")<br>
> AttributeError: 'list' object has no attribute 'name'<br>
<br>
This is because you are accessing the method via<br>
the class and passing farmlet as the instance value<br>
rather than sending the message to the innstance<br>
directly. Use<br>
<br>
critter.talk() # and no farmlet needed!<br>
<br>
HTH,<br>
<br>
<br>
--<br>
Alan Gauld<br>
Author of the Learn to Program web site<br>
<a href="http://www.alan-g.me.uk/" target="_blank">http://www.alan-g.me.uk/</a><br>
<br>
<br>
<br>
<br>
</blockquote></div><br>