Python: Write the definition of a class Counter containing
Python: Write the definition of a class Counter containing
Ok so I hate MPL with a passion, and this problem is exactly why. Heres the solution:
class Counter(object):
counter = 0
def __init__(self, counter = 0):
self.__counter = counter
def increment(self):
self.__counter += 1
def decrement(self):
self.__counter -= 1
def get_value(self):
return self.__counter
Counter()
So really the only big error you made was not using localization: self.__counter
But what MPL wants is for you to add Counter()
to the end of the assignment even though it does not indicate this or has it required it in previous assignments to this point. This post may be old but this problem is still present.
Proof of it being accepted here.
Proof of the same code being rejected
class Counter(object):
def __init__(self):
self.counter = 0
def increment(self):
self.counter += 1
def decrement(self):
self.counter -= 1
def get_value(self):
return self.counter
Python: Write the definition of a class Counter containing
They want you to type the code a certain way. Youre not necessarily wrong its just not what theyre looking for.
class Counter:
def __init__(self, counter=0):
self.__counter =counter
def increment(self):
self.__counter +=1
def decrement(self):
self.__counter -=1
def get_value(self):
return self.__counter