Can someone explain me what is going on?


#1
class Animal(object):
    """Makes cute animals."""
    is_alive = True
    health="good"
    sloth=Animal("jok",14)
    
    def __init__(self,name,age):
        self.name=name
        self.age=age
        
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def description(self):
        print self.name
        print self.age

hippo=Animal("hippy",30)
hippo.description()
   
print sloth.health
print ocelot.heatlh
print hippo.health

#2

Perhaps if you told us what behaviour you want explained and posted the intact code.


#3

First, I agree with ionatan. Please, give reference to the section of the unit you are on. Second, ask where you are stuck or where you might think the problem is. Normally, I would not help because you didn't put in the effort to ask correctly.

So anyways, heres help regardless.

(1) Right away, I can see you tried to create an object inside your class. To instantiate an object you need to place the new sloth variable outside of class. So sloth = Animal("jok", 14) needs to be below the Animal class, and it needs to have no indent.

(2) Second, you have two init functions. Get rid of one.

(3) You have to declare the sloth variable and instantiate it as well. So, by your hippo variable (and where your going to move your sloth variable) add an ocelot variable with a name and age.

Do that and it should work.


#4

This post is about ten days old, but I think some people need to hear this.

If you don't feel like reading this, this is just "If you can't help without being an ■■■, let someone else help." summed up. There's a better way to say. "You should ask a question this way, instead of that way. It will help us answer your question better." than the way you guys choose to do it. Assume that people are frustrated and crushed when they come to forums to ask questions. No need to crush them further, your job is already done.

I do not agree with ionatan's way of getting his point across in this reply, and pretty much every reply I've seen him make.

You guys, and everyone on the internet who makes these comments need to stop thinking that the person asking the question is someone openly disrespecting you, this site, or... umm... (the sacred art of coding?) by asking questions that don't meet your standards, or whatever easily-overlooked-when-you-are-bewildered-by-a-jungle-of-code standard you may believe that people should instantly be aware of..

I think you guys need to remember what it was like to be a beginner. I would say that 9/10 people come to the forums frustrated, because their dream, the reason they wanted to start coding, is halted by a difficult concept. Their dream is halted and they are frustrated. The last thing they need is a response that resembles criticism from a snarky, entitled smart alec.

I mean this more for ionatan. at least gangware attempted to help. ionatan, I recommend you either stop "helping", or start being a good teacher. You're demanding some form or respect by saying "I won't help you unless you do this." This has the opposite effect when the question is clearly this: "Can someone explain to me what is going on?", and the The lesson is right under the question. Python: 19. Introduction to Classes, and makes me want to slap you.

If you are unsure what the person is asking even after all that. Well, why do people come to the forums... they're stuck.. their code isn't working and they're wondering what is wrong. Is anyone not asking that question?

I've only been coding for 2 weeks and already I felt like answering that this lesson is showing you how to use member variables. which they say are variables available to all members of a class. (I wish I could simplify that but I don't know enough about this to do that yet). Don't worry if you find this confusing because I had to look over the previous pages (1-9) several times before I completed this one. This lesson is just having you print out the values stored in the member variables.

gangware did explain what was wrong with the code, but I, as a beginner, had to look back and read what instantiate meant. (it takes beginners a while before they start speaking in these terms they would otherwise never use in common conversation)

\\
''class Animal(object):
"""Makes cute animals."""
is_alive = True
health = "good"
def init(self, name, age):
self.name = name
self.age = age
# Add your method here!
def description(self):
print self.name
print self.age

hippo = Animal("Hippo the hippo", 10)
sloth = Animal("Sloth the sloth", 7)
ocelot = Animal("Ocelot the ocelot", 8)

hippo.description()

print hippo.health
print sloth.health
print ocelot.health \\

that code worked for me.

People come here because they are frustrated and need help, don't make them more frustrated. You wouldn't like that, they wouldn't like that. No one likes that.

ionatan - "Perhaps if you told us what behaviour you want explained and posted the intact code."
If english isn't your first language, then I understand if you speak this way, and nevermind. If you know english perfectly well and still choose to speak this way please rethink your desire to help others. This sounds cold and robotic and turns beginners away.

gangware "Normally, I would not help because you didn't put in the effort to ask correctly." People who come to these forums are frustrated, not everyone has an endless pool of focus and calm to draw from whenever they want.