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