我需要帮助.append

时间:2017-01-15 18:36:38

标签: python class append

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)

1 个答案:

答案 0 :(得分:0)

在构造函数中设置self.code 2次(2. time = [])。

我还添加了一个标志open来保存锁的状态。 检查if print == "open"无法正常工作。

此外,无需保存self.enteredCombination= enteredCombination,因为您只需要在方法中使用它。

如果combinationint,则无需使用removeappend

class Padlock:
    def __init__(self, combination):
        self.code = combination
        self.open = True

    def openLock(self, enteredCombination):
        if self.code == enteredCombination:
            self.open = True
            print("open")
        else:
            self.open = False
            print("closed")

    def changeCombination(self, newCombination):
        if self.open:
            print("type in new code")
            self.code = newCombination
        else:
            print("open lock first")

code = "1234"
l = Padlock(code)
l.openLock(code)

newCode = "5678"
l.changeCombination(newCode)
l.openLock(code)
l.changeCombination(newCode)

l.openLock(newCode)