Discussion:
[Tutor] create class Pet
Stephanie Quiles
2015-06-16 16:45:06 UTC
Permalink
Hello, Having trouble figuring out why this program is not running. could someone please take a look and see where I am going wrong? Here is the error message i am getting : /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/stephaniequiles/PycharmProjects/untitled3/pets.py
Traceback (most recent call last):
File "/Users/stephaniequiles/PycharmProjects/untitled3/pets.py", line 2, in <module>
def main(get_name=name):
NameError: name 'name' is not defined

Process finished with exit code 1

Thanks !!!!

Code is below:


# __author__ = 'stephaniequiles'

# write a class named Pet should include __name, __animal_type, __age

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'
def main():
name = input('what is the name of the pet?: ')
animal_type = ('Please enter a type of pet: ')
age = int(input('Enter age of pet: '))
self.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()






_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-16 18:10:21 UTC
Permalink
Post by Stephanie Quiles
File "/Users/stephaniequiles/PycharmProjects/untitled3/pets.py", line 2, in <module>
NameError: name 'name' is not defined
There is an inconsistency here.
The code you have shown does not match the error.
The error says the def main statememt looks like

def main(get_name=name):

and is on line 2 of pets.py.

But your def main is near the end of the file and has no
get_name parameter.

Either you are running the wrong file or you have changed it.
Until the error matches the code there is little else we
can do.
Post by Stephanie Quiles
# __author__ = 'stephaniequiles'
# write a class named Pet should include __name, __animal_type, __age
# pet class should have an __init__ method that creates these attributes.
self.__name = 'name'
self.__animal_type = 'animal_type'
self.__age = 'age'
self.__name = 'name'
self.__animal_type = animal_type
self.__age = age
return self.__name
return self.__animal_type
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'
name = input('what is the name of the pet?: ')
animal_type = ('Please enter a type of pet: ')
age = int(input('Enter age of pet: '))
self.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()
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-06-16 19:49:08 UTC
Permalink
Post by Stephanie Quiles
Hello, Having trouble figuring out why this program is not running. could someone please take a look and see where I am going wrong? Here is the error message i am getting : /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/stephaniequiles/PycharmProjects/untitled3/pets.py
File "/Users/stephaniequiles/PycharmProjects/untitled3/pets.py", line 2, in <module>
NameError: name 'name' is not defined
Process finished with exit code 1
Thanks !!!!
# __author__ = 'stephaniequiles'
# write a class named Pet should include __name, __animal_type, __age
# pet class should have an __init__ method that creates these attributes.
self.__name = 'name'
self.__animal_type = 'animal_type'
self.__age = 'age'
self.__name = 'name'
Further to Alan's answer the above methods are wrong. You're setting
all the instance variables to strings instead of the actual variable
names. Get rid of the single quotes.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Stephanie Quiles
2015-06-16 20:15:28 UTC
Permalink
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/stephaniequiles/PycharmProjects/untitled3/pets.py
what is the name of the pet?: riley
Please enter a type of pet: cat
Enter age of pet: 11
Traceback (most recent call last):
File "/Users/stephaniequiles/PycharmProjects/untitled3/pets.py", line 15, in <module>
main()
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'

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()
Post by Stephanie Quiles
Hello, Having trouble figuring out why this program is not running. could someone please take a look and see where I am going wrong? Here is the error message i am getting : /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/stephaniequiles/PycharmProjects/untitled3/pets.py
File "/Users/stephaniequiles/PycharmProjects/untitled3/pets.py", line 2, in <module>
NameError: name 'name' is not defined
Process finished with exit code 1
Thanks !!!!
# __author__ = 'stephaniequiles'
# write a class named Pet should include __name, __animal_type, __age
# pet class should have an __init__ method that creates these attributes.
self.__name = 'name'
self.__animal_type = 'animal_type'
self.__age = 'age'
self.__name = 'name'
Further to Alan's answer the above methods are wrong. You're setting all the instance variables to strings instead of the actual variable names. Get rid of the single quotes.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-17 07:40:40 UTC
Permalink
Post by Stephanie Quiles
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.
Post by Stephanie Quiles
Process finished with exit code 1
# pet class should have an __init__ method that creates these attributes.
self.__name = "name"
self.__animal_type = "animal_type"
self.__age = "age"
self.__name = "name"
self.__animal_type = animal_type
self.__age = age
return self.__name
return self.__animal_type
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
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Stephanie Quiles
2015-06-17 19:43:55 UTC
Permalink
You are right I don't understand functions very well still. This prompts the next question is there a book or online resource that you suggest I look at? Right now I am using a text book provides by the instructor called Starting Out With Python 3rd ed.
Anything you can suggest I reference would be appreciated. I will review the code when I get home.

Thanks
Stephanie Quiles
Sent from my iPhone
Post by Alan Gauld
Post by Stephanie Quiles
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
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...
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
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
self.__name = "name"
where it should be
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.
Post by Stephanie Quiles
Process finished with exit code 1
# pet class should have an __init__ method that creates these attributes.
self.__name = "name"
self.__animal_type = "animal_type"
self.__age = "age"
self.__name = "name"
self.__animal_type = animal_type
self.__age = age
return self.__name
return self.__animal_type
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
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
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-17 23:16:23 UTC
Permalink
Post by Stephanie Quiles
You are right I don't understand functions very well still. This prompts the next question is there a book or online resource that you suggest I look at? Right now I am using a text book provides by the instructor called Starting Out With Python 3rd ed.
Anything you can suggest I reference would be appreciated. I will review the code when I get home.
I'm obviously biased, but you could try mine(see .sig)
There are topics on "functions and modules" and "OOP"
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...