79729370

Date: 2025-08-08 05:44:52
Score: 1
Natty:
Report link

Use a @property instead of a method

This makes "is_locked" behave like a variable, not a function:

class Lock:
    def __init__(self):
        self.key_code = "1234"
        self._is_locked = True  # underscore to mark internal

    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.")

    @property
    def is_locked(self):
        return self._is_locked


def main():
    my_lock = Lock()

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

    print("The lock is now unlocked.")
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @property
  • Low reputation (1):
Posted by: Ammar Yazdani