Write the definition of a class
Counter
containing:写一个包含以下类的计数器的定义:
- An instance variable named
counter
of typeint
.名为counter的类型为int的实例变量。
- A constructor that takes one
int
argument and assigns its value tocounter
一个构造函数,它接受一个int参数并将其值赋给counter
- A method named
increment
that adds one tocounter
. It does not take parameters or return a value.一个名为increment的方法,它向计数器添加一个。它不接受参数或返回值。
- A method named
decrement
that subtracts one fromcounter
. It also does not take parameters or return a value.一个名为decrement的方法,它从计数器中减去一个。它也不带参数或返回值。
- A method named
get_value
that returns the value of the instance variablecounter
.一个名为get_value的方法,它返回实例变量counter的值。
class Counter(object):
def __init__(self, counter):
self.counter = counter
def increment(self):
self.counter += 1
def decrement(self):
self.counter -= 1
def get_value(self):
return self.counter
that's my answer but turningscraft says that it's incorrect and my homework is due soon :/ so please help
这是我的答案,但是turnscraft说这是不正确的,我的作业即将到期:/所以请帮忙
4 个解决方案
#1
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
#2
They want you to type the code a certain way. You're not necessarily wrong it's just not what they're 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
#3
class Counter:
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
This solution works but it does not set up the constructor the way I thought the question asked you to do it.
这个解决方案有效,但它没有按照我认为问题的方式设置构造函数。
#4
Ok so I hate MPL with a passion, and this problem is exactly why. Here's the solution:
好的,所以我讨厌激情的MPL,这个问题就是为什么。这是解决方案:
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
所以你真正唯一的错误就是没有使用本地化: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.
但MPL想要的是你将Counter()添加到赋值的结尾,即使它没有表明这一点,或者它在之前的赋值中是否需要它。这篇文章可能很旧,但这个问题仍然存在。
Proof of it being accepted here. Proof of the same code being rejected
它的证明在这里被接受。被拒绝的相同代码的证明
#1
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
#2
They want you to type the code a certain way. You're not necessarily wrong it's just not what they're 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
#3
class Counter:
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
This solution works but it does not set up the constructor the way I thought the question asked you to do it.
这个解决方案有效,但它没有按照我认为问题的方式设置构造函数。
#4
Ok so I hate MPL with a passion, and this problem is exactly why. Here's the solution:
好的,所以我讨厌激情的MPL,这个问题就是为什么。这是解决方案:
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
所以你真正唯一的错误就是没有使用本地化: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.
但MPL想要的是你将Counter()添加到赋值的结尾,即使它没有表明这一点,或者它在之前的赋值中是否需要它。这篇文章可能很旧,但这个问题仍然存在。
Proof of it being accepted here. Proof of the same code being rejected
它的证明在这里被接受。被拒绝的相同代码的证明