【发布时间】:2023-04-06 04:20:01
【问题描述】:
openLock 没有打印打开,它一直显示关闭。请问我的代码有什么问题。谢谢
class Padlock:
def __init__(self, combination):
self.code = combination
self.code=[]
def openLock(self,enteredCombination):
self.enteredCombination= enteredCombination
if self.code == self.enteredCombination:
print("open")
else:
print("closed")
def changeCombination(self, newCombination):
if print == "open":
print("type in new code")
self.code.remove([0])
self.code.append(newCombination)
else:
print("open lock first")
lock1=Padlock(1234)
lock1.openLock(1234)
【问题讨论】:
-
这段代码怎么称呼?我看到的唯一想法是类定义。 Python 本身不会对类定义做任何事情。
-
lock1=挂锁(1234),lock1.openLock(1234),lock1.(4444)。谢谢
-
Edit 提供minimal reproducible example 的问题,实际上解释了问题。
-
@thecodesalim: 但是你只调用不打印的
__init__方法。此外,在您的__init__方法中,您只需再次覆盖self.code。