Python · Exercise 6 of 6 Previous
Advanced
Write a context manager that always cleans up
Using @contextmanager, write logged(name). On entering the with block it prints enter <name>; on leaving it prints exit <name>, even if the block raises an error. The error must still reach the caller. It should yield the name in upper case.
Use it around a print("working") and print the yielded name.
What Check tests
- Normal use prints enter, the body, then exit
- It yields the name in upper case
exitis printed even when the body raises- The error still reaches the caller
- The program prints the four lines
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.