try, except and finally, and returns a value from two places.
class Door:
def __init__(self):
self.log = []
def open(self, key):
try:
if key != 7:
raise PermissionError
self.log.append("open")
return True
except PermissionError:
self.log.append("denied")
return False
finally:
self.log.append("checked")
d = Door()
print(d.open(7), d.open(1), d.log)
What does the program print?Log in to join the discussion.
Loading…