[Tutor] create class Pet

Alan Gauld alan.gauld at btinternet.com
Wed Jun 17 09:40:40 CEST 2015


On 16/06/15 21:15, Stephanie Quiles wrote:
>> sorry this is the correct error.


>    File "/Users/stephaniequiles/PycharmProjects/untitled3/pets.py", line 7, in main
>      pet.get_name(name, animal_type, age)
> AttributeError: 'module' object has no attribute 'get_name'

There are several errors in this line, all of which suggest
you don't really understand what you are doing with functions, #
classes and objects. You need to re-read your tutorial material
more closely.

1) Starting with the reported error.
You called

pet.get_name(...)

pet is the name of your file so Python sees that as a module.
But get_name is a method of your Pet class. The class name is
captalized and case matters in Python. 'Pet' is not the
same as 'pet' That's why it says there is no such module
attribute as get_name.

2) The method call has 3 arguments: name, type and age.
But your method definition has no attributes (apart
from the obligatory self). When you call a function
(or method) you must only include the arguments that
the function definition expects. So your call to
get_name() would have failed even if you had not
misspelled pet.

3) get_name() returns a string value - the __name of the pet.
You call get_name() but do not use or store the result so
it is thrown away. I suspect you meant to print the result
so you should have written something like:
print ( my_pet.getname() )

4) get_name() is a method of the class Pet. That means
you should call it as an attribute of an object which
is an instance of Pet. That is, you must create an
instance of Pet before you try to use any of its methods.
You did not create any instances. Interestingly, your
__init__() method does take the 3 parameters that
you tried to pass to get_name(). This means that
you could have replaced the get_name() call with

my_pet = Pet(name, animal_type, age)

Now that we have dealt with that line lets move on
to the rest of your main function...

You have several lines like:

      print("Pet Name: ", pet.get_name)

The problem here is that you are passing the method name
into the print function. You are not *calling* the method.
Also you are using the module name (pet) to access get_name,
but it needs to be an instance of Pet - see above.

To do all that you must use abn instance and put parentheses
after the method name, so it should look like:

      print("Pet Name: ", my_pet.get_name() )

The final set of errors have already been highlighted by Mark.
Namely where you set attribute values in the class methods
you are creating strings instead of using the variables.
ie you are writing

       def set_name(self, name):
           self.__name = "name"

where it should be

      def set_name(self, name):
          self.__name = name

with no quote signs.

If you make all those changes I think it should work.
However, given the number and nature of the errors, I cannot
help but think you need to go back and re-read your
tutorial material. Details are very important in programming
and you seem to still be confused about naming, function definitions and 
calling and the relationship between classes and objects/instances.

>
> Process finished with exit code 1
>
> class Pet:
>      # pet class should have an __init__ method that creates these attributes.
>      def __init__(self, name, animal_type, age):
>          self.__name = "name"
>          self.__animal_type = "animal_type"
>          self.__age = "age"
>
>      def set_name(self, name):
>          self.__name = "name"
>
>      def set_type(self, animal_type):
>          self.__animal_type = animal_type
>
>      def set_age(self, age):
>          self.__age = age
>
>      def get_name(self):
>          return self.__name
>
>      def get_animal_type(self):
>          return self.__animal_type
>
>      def get_age(self):
>          return self.__age
>
> # create methods,  set_name , set_animal_type, set_age,  get_name, get_animal_type
> # get_age
>
> # write a program that creates an object of the class and prompts the use to enter
> # name, type, age of their pet.
>
> # data should be stored as objects attributes.
> # use objects accessor methods to retrieve the pet's name, type and age
> # display data on screen
>
> # __author__ = 'stephaniequiles'
> import pet
> def main():
>      name = input("what is the name of the pet?: ")
>      animal_type = input("Please enter a type of pet: ")
>      age = input("Enter age of pet: ")
>      pet.get_name(name, animal_type, age)
>      print("This will be saved to file.")
>      print("Here is a the data you entered: ")
>      print("Pet Name: ", pet.get_name)
>      print("Animal Type:", pet.get_animal_type)
>      print("Age: ", pet.get_age)
>
>
> main()

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list