在 Python 中创建一个简单的锁和键函数

Create a simple lock and key function in Python

提问人:Jenson 提问时间:8/4/2023 最后编辑:quamranaJenson 更新时间:8/9/2023 访问量:138

问:

class Lock:
 
   def __init__(self):
        self.key_code = "1234"  # Change this to your desired key code
        self.is_locked = True

    def lock(self):
        self.is_locked = True

    def unlock(self, entered_code):
        if entered_code == self.key_code:
            self.is_locked = False
        else:
            print("Incorrect key code. Lock remains locked.")

    def is_locked(self):
        return self.is_locked


def main():
    my_lock = Lock()

    print("The lock is currently locked.")
    while my_lock.is_locked():
        entered code = input("Enter the key code: ")
        my_lock.unlock(entered code)

    print("The lock is now unlocked.")


if __name__ == "__main__":
    main()

我写了这段代码,但它显示了一个

Type Error: 'bool' object is not callable
The lock is currently locked.

我一直在尝试创建一个锁定机制,但它显示类型错误。我尝试了很多方法,但我仍然无法解决它。

python-3.x 错误处理 锁定

评论


答:

1赞 JaySean 8/4/2023 #1

你快到了:你的代码有轻微的错误 i. 构造函数方法应命名为 init(带有双下划线)而不是 init。 ii. 您同时具有一个名为 is_locked 的实例变量和一个名为 is_locked 的方法。这可能会导致冲突。我将方法重命名为 is_locked_status 以避免冲突。 iii. 调用 is_locked_status 方法时,需要使用括号 () 调用该方法并获取其返回值。

class Lock:
    def __init__(self):  # Use double underscores for the constructor method name
        self.key_code = "1234"
        self.is_locked = True

    def lock(self):
        self.is_locked = True

    def unlock(self, entered_code):
        if entered_code == self.key_code:
            self.is_locked = False
        else:
            print("Incorrect key code. Lock remains locked.")

    def is_locked_status(self):  # Rename the method to avoid name conflict
        return self.is_locked

def main():
    my_lock = Lock()

    print("The lock is currently locked.")
    while my_lock.is_locked_status():  # Call the renamed method
        entered_code = input("Enter the key code: ")
        my_lock.unlock(entered_code)
    print("The lock is now unlocked.")

if __name__ == "__main__":
    main()

评论

0赞 chepner 8/4/2023
is_locked最好用作谓词方法的名称。由于没有人应该直接访问 instance 属性(即,在不使用方法的情况下进行设置),因此 instance 属性最好命名(或者实际上只是)。my_lock.is_locked = Falseunlock_is_locked_locked
1赞 rkochar 8/4/2023 #2

这里有 2 种方法可以做到这一点。

class Lock:

    def __init__(self):
        self.key_code = "1234"  # Change this to your desired key code
        self.is_locked = True

    def lock(self):
        self.is_locked = True

    def unlock(self, entered_code):
        if entered_code == self.key_code:
            self.is_locked = False
        else:
            print("Incorrect key code. Lock remains locked.")

    def is_locked(self):
        return self.is_locked


def main():
    my_lock = Lock()

    print("The lock is currently locked.")
    while my_lock.is_locked:  # <-- Use variable here instead of getter
        entered_code = input("Enter the key code: ")
        my_lock.unlock(entered_code)

    print("The lock is now unlocked.")


if __name__ == "__main__":
    main()

这可行,但您想使用 getter 来访问您的变量。

class Lock:

    def __init__(self):
        self.key_code = "1234"  # Change this to your desired key code
        self.locked = True

    def lock(self):
        self.locked = True

    def unlock(self, entered_code):
        if entered_code == self.key_code:
            self.locked = False
        else:
            print("Incorrect key code. Lock remains locked.")

    def is_locked(self):
        return self.locked


def main():
    my_lock = Lock()

    print("The lock is currently locked.")
    while my_lock.is_locked():
        entered_code = input("Enter the key code: ")
        my_lock.unlock(entered_code)

    print("The lock is now unlocked.")


if __name__ == "__main__":
    main()

问题在于变量和方法具有相同的名称。提示在错误中 - 它使用变量认为它是一个方法,而不是使用具有相同名称的方法。更改变量以使其正常工作。locked