我和我的伴侣已经为此工作了几个小时,无法解决。方向在某些领域含糊不清,我们的教授没有很好地分解它以帮助我们。这是指南的链接。我相信他们不太清楚,但是如果我做错了,请改正我,请多加思考https://imgur.com/a/huHnwos
我相信我们最大的问题是unlock(combination)和set_new_combination(new_combination)方法。我可以弄清楚 str ()方法,因为它并不是很难做到的。我们已经尝试了教授告诉我们的尝试,但是没有成功。
class Lock:
def __init__(self, combination = 0):
self.combination = combination
self.locked = False
def lock(self):
self.locked = True
def unlock(self, combination):
if combination == True or combination == 0:
self.locked = False
def set_new_combination(self, new_combination):
if self.locked == False:
self.combination = new_combination
def is_locked(self):
if self.locked == True or self.combination == True:
return True
else:
return False
def __eq__(self, other):
if other is not None and type(other) == type(self):
if self.combination == other.new_combination:
return False
def __str__(self):
return self.combination, ',', self.locked
预期结果应该是可以正常工作的基本密码锁。
答案 0 :(得分:0)
您的unlock
方法正在尝试将布尔值与数字(组合)进行比较。更改为如下所示:
def unlock(self, combination):
if combination == self.combination:
self.locked = False
您还可以通过is_locked
方法执行此操作,因此也应该更改此内容:
def is_locked(self):
return self.locked
(每次发现自己写成if x return True else return False
的东西时,只要条件简单,几乎总是可以用return x
代替)。
set_new_combination
工作正常;我不知道您看到了什么问题。
最后,您的__str__
方法应该实际上返回一个字符串:
def __str__(self):
return '[' + str(self.combination) + ', ' + 'locked' if self.locked else 'unlocked' + ']'
答案 1 :(得分:0)
您的代码有几个问题。首先,只有在combination == 0 or combination == 1
才执行解锁方法中的if语句,这与锁的组合(self.combination
)没有关系。在is_locked方法中,您仅应返回self.locked
,而无需返回if
。 __eq__
方法也可以简化。并且__str__
方法实际上应该返回String。
class Lock:
def __init__(self, combination = 0):
self.combination = combination
self.locked = False
def lock(self):
self.locked = True
def unlock(self, combination):
if self.combination == combination:
self.locked = False
def set_new_combination(self, new_combination):
if not self.locked:
self.combination = new_combination
def is_locked(self):
return self.locked
def __eq__(self, other):
return isinstance(other, Lock) and self.combination == other.combination
def __str__(self):
return f'{self.combination}, { "locked" if self.locked else "unlocked"}'
答案 2 :(得分:0)
这是我基于提供的指令的实现,带有注释的地方与代码有出入。
class Lock:
def __init__(self, combination = 0): # No change here
self.combination = combination
self.locked = False
def lock(self):
# Although a test of self.locked is redundant, the instructions state
# "...if invoked a second time this, method should do nothing."
if not self.locked:
self.locked = True
def unlock(self, combination):
# You were not testing the stored combination against the one passed to the method.
# it does not matter if the stored combination is zero or a different number,
# you still need to check for equality.
# You also need a test as with lock() to satisfy the "if invoked a second time this,
# method should do nothing" requirement.
if self.locked and self.combination == combination:
self.locked = False
def set_new_combination(self, new_combination):
# You can simply the `if` condition, there's no need to test against False
if not self.locked:
self.combination = new_combination
def is_locked(self):
# I don't know why you are testing the self.combination value, you
# only need to return the state of the lock
return self.locked
def __eq__(self, other):
# You have the correct guard conditions but were returning False when
# the combinations matched. You can simply return the comparison result.
if other is not None and type(other) == type(self):
return self.combination == other.new_combination
def __str__(self):
# For some reason the output format specified for this appears to put it in a list
# (the square brackets) but as it's only for display we'll "fake" the list.
# The `if` statement prints the word 'locked' or 'unlocked' depending on the
# `self.locked` state.
return '[{}, {}]'.format(self.combination, 'locked' if self.locked else 'unlocked')