[Tutor] create class Pet
Stephanie Quiles
stephanie.quiles001 at albright.edu
Tue Jun 16 22:15:28 CEST 2015
> sorry this is the correct error. it allows me to enter name and age but then i get the message:
/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()
> On Jun 16, 2015, at 3:49 PM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
>
> On 16/06/2015 17:45, Stephanie Quiles wrote:
>> 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'
>
> 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 - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list