0

I am supposed to use setters and getters for this class instance but getting the following error after I prompt user for input.

Traceback (most recent call last): File "python", in File "python", in init File "python", in Name File "python", in Name File "python", in Name [Previous line repeated 492 more times] RecursionError: maximum recursion depth exceeded

class Pet:

  def __init__(self, nameOfPet, typeOfPet, ageOfPet):
    self.Name = nameOfPet
    self.Types = typeOfPet
    self.Age = ageOfPet

  @property
  def Name(self):
    return self.Name

  @Name.setter
  def Name(self, petName):
    self.Name = petName

  @property
  def Types(self):
    return self.Types

  @Types.setter
  def Types(self, petType):
    self.Types = petType

  @property
  def Age(self):
    return self.Age

  @Age.setter
  def Age(self, petAge):
    self.Age = petAge

  def printPetInfo(self):
    print("Hello. Your pet's name is {}, it is a {} type and it is {} years old. ".format(self.Name,self.Types,self.Age))

#Get input from user.
name = input("Enter the pet's name: ")
types = input("Enter the pet's type (Dog, Cat, Bird, etc.): ")
age = input("Enter the pet's age (in years): ")
while age.isalpha():
  print()
  print("Age cannot contain numbers. Try again.")
  age = input("Enter the pet's age (in years): ")

#Instantiate a new Pet Instance.
myPet = Pet(name,types,age)

myPet.printPetInfo()

print("\n \n \n \n \n \n")
Cornel
  • 180
  • 2
  • 4
  • 13
  • 1
    This is not a use-case for properties yet. You should never write a Python class definiton like this. – juanpa.arrivillaga Aug 16 '17 at 22:46
  • 1
    As @juanpa.arrivillaga already said, there is no need for getters or setters here. Simply writing getters and setters out of convention is a very bad idea. – Christian Dean Aug 16 '17 at 22:47
  • I'm assuming this is some sort of homework. You should do your homework like your professor tells you to. But please note, this is not how you should be doing things in Python in the real world. I think, unfortunately, there are a ton of intro-cs classes that switched languages to Python from Java, and essentially, a lot of professors are teaching Java in Python... – juanpa.arrivillaga Aug 16 '17 at 22:49
  • It's an exercise from a book that teaches pseudocode which I'm supposed to translate to Python syntax. – Cornel Aug 16 '17 at 22:54
  • Well, the "correct" way to go about this in Python would be if you deleted every method except `__init__` and `printPetInfo`. Also, you should try to stick to official style conventions, so `snake_case` for variable and function names, `UpperCase` only for class names... And no `camelCase`. – juanpa.arrivillaga Aug 16 '17 at 23:02
  • Read [this](http://www.python-course.eu/python3_properties.php) if you want to understand the Python way of doing things. – juanpa.arrivillaga Aug 16 '17 at 23:09

0 Answers0