i need help on .append

thecodesalim

openLock is not printing open, it keeps showing close. please whats the problem with my code. thank you

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)
ppasler

You set self.code 2 times in the constructor (2. time = []).

I also added a flag open to save the state of the lock. The check if print == "open" does not work as you might expect it.

Also there is no need to save self.enteredCombination= enteredCombination, as you only need it inside the method.

If combination is an int, there is no need to use remove and append.

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related