3

I was advised to re post this to be more clear.

Doing a class and finished with the rest less this one. Any guidance is appreciated. I have derived part of the question where I am stuck with to keep it short. I have also attached my working.

With my working below, I expect to be able to create a class containing one variable. I want to be able to change that variable and print the new variable. Example, change the value from horns = 2 to horns = 4. The question asks me specifically to use the 3 functions below to answer the question. With my current codes, I get an error message after I enter the value at the raw_input prompt.

Thanks in advance for help.

Question as follows:

Create a class with 1 variable in it holding its own properties. Provide the following 3 methods:

getvariable1() - use return key to return value of property 1

setvariable1() - This should allow new value to be specified for property 1 - additional parameter needed to accept input.

printerfun() - to print values of the variables for the object.

Create your own object of the class and call get & set methods for the object created. Use printerfun() method to check if the codes works.

My working:

class animal:
    horns = 2

    def printerfun(self):
        print getHorns() 

    def getHorns(self): #don't get where I should call this
        return self.horns

    def setHorns(horns): 
        self.horns = horns

animal_1 = animal()

F1 = raw_input('Please enter number of horns: ')
setHorns(F1) 
1
  • @DanielRoseman I think s/he may just be using this sort of thing to introduce OOP? Teaching OOP principles is not the same as teaching python, and, generally, the concept of interface is a useful one. Commented Jan 30, 2013 at 20:53

2 Answers 2

1

Is this what you want to obtain?

class animal: 
    horns = 2

    def printerfun(self):
        print self.getHorns() 

    def getHorns(self):
        return self.horns

    def setHorns(self, horns): 
        self.horns = horns

if __name__ == "__main__"
    animal_1 = animal()
    animal_1.printerfun()

    F1 = raw_input('Please enter number of horns: ') 
    animal_1.setHorns(F1)
    animal_1.printerfun()
    horns = animal_1.getHorns()
    print(horns)

This outputs:

>>> 2
>>> Please enter number of horns: 4
>>> 4
>>> 4
Sign up to request clarification or add additional context in comments.

1 Comment

I do get the idea of class behaving as a blue print under which objects are given. We can create further instances from this blueprint and create unique objects. More or less my grasp, hope am right. It is the question that I am unable to figure out answering it using the 3 functions given.
1

setHorns doesn't exist: animal.setHorns does. It's a class method

It sems you need to read a bit about Object Oriented Programming, which is the style of programming used when there's classes involved.

In this particular exercise, you need to create a animal object. For that, you need to instantiate the class. This is what you've done with animal_1 = animal(). animal_1 is now a object of the animal class, and you can call its methods: animal_1.setHorns(2)

If you're still struggling with the concepts, you may want to read a more ground to earth tutorial in python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.