A finally that runs before the return
Predict the output · Medium · §13.4 Cleaning up and refusing honestly
This function looks up the number of likes by name. Predict the output without running the code.
Code
likes = {"mina": 120, "jun": 45}
def find(name):
try:
n = likes[name]
except KeyError:
print("Not found:", name)
return None
else:
print("Found:", name)
return n
finally:
print("Lookup done")
print(find("jun"))
print(find("taeo"))Solve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.