2017-01-15 18 views
-1

openLockは開いていませんが、それは近くに表示され続けます。私のコードの問題は何ですか?ありがとう私は助けが必要です.append

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) 
+0

このコードはどのように呼び出しますか?私が見るのはクラス定義だけです。 Pythonはクラス定義で何もしません。 –

+0

lock1 =南京錠(1234)、lock1.openLock(1234)、lock1。(4444)。ありがとうございます – thecodesalim

+0

[編集] *実際に問題を説明している[mcve]を与える質問*。 – jonrsharpe

答えて

0

self.codeをコンストラクタで2回(2回= [])に設定しました。

また、ロックの状態を保存するために、フラグopenを追加しました。 if print == "open"のチェックは期待通りに機能しません。

self.enteredCombination= enteredCombinationを保存する必要はありません。メソッド内でのみ必要なので

combinationintである場合、removeおよび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) 
+0

ありがとうございました。 – thecodesalim

+0

@thecodesalim答えを受け入れ、今質問を締めてください。 – ppasler

関連する問題