0
class Vehicle:

    def __init__(self, make, model):
        self.year = 2000
        self.make = make
        self.model = model

    @property
    def year(self):
        return self._year

    @year.setter
    def year(self, value):
        if (value > 2000 and value < 2018):
            self._year = value


    @property
    def make(self):
        return self._make

    @make.setter
    def make(self, value):
        self._make = value

    @property
    def model(self):
        return self._model

    @model.setter
    def model(self, value):
        self._model = value


# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
# the main part of the program
v1 = Vehicle("Dodge", "Ram")
v2 = Vehicle("Honda", "Odyssey")
print "v1={} {}".format(v1.make, v1.model)
print "v2={} {}".format(v2.make, v2.model)
print

v1.year = 2016
v2.year = 2016
print "v1={} {} {}".format(v1.year, v1.make, v1.model)
print "v2={} {} {}".format(v2.year, v2.make, v2.model)
print

v1.year = 1999
v2.model = "Civic"
v2.year = 2007
print "v1={} {} {}".format(v1.year, v1.make, v1.model)
print "v2={} {} {}".format(v2.year, v2.make, v2.model)

I need help implementing a mutator and accessor for the year variable. The instructions are as follows:

(1)The constructor must take two parameters, make and model, and make proper use of mutators to set these values; (2) By default, a newly instantiated vehicle has a year of 2000; (3) Accessors and mutators (using the decorator method discussed in class) for each instance variable (i.e., year, make, and model) must be included; (4) A vehicle must have a year that is between 2000 and 2018 inclusive (i.e., implement range checking so that any other provided value is ignored)

The correct output should be:

v1=Dodge Ram v2=Honda Odyssey

v1=2016 Dodge Ram v2=2016 Honda Odyssey

v1=2016 Dodge Ram v2=2007 Honda Civic

My output however:

v1=Dodge Ram v2=Honda Odyssey

v1=2016 Dodge Ram v2=2016 Honda Odyssey

v1=1999 Dodge Ram v2=2007 Honda Civic

The accessor and mutator for year is not correctly checking for the range in which the value is acceptable. I'm still quite new to programming so I apologize if this is a simple question/simple fix!!

setho14
  • 5
  • 2

1 Answers1

0

Your mutators and accessors are not working because you are using the classic style of classes.

For properties to work, you should use the new style of class where you inherit from 'object'.

You can find out more information about the differences between the two class styles here: What is the difference between old style and new style classes in Python?

To fix your code, change:

class Vehicle:

Into This:

class Vehicle(object):
Thomas English
  • 231
  • 1
  • 8
  • Thank you, that worked. I had a feeling I overlooked something small like that. Much appreciated – setho14 Mar 27 '18 at 18:21
  • You're welcome. I've added a link to my answer if you want to learn more about the differences between the two styles of classes. You should always default to using the newer style. – Thomas English Mar 27 '18 at 18:26