contextlib.contextmanager
Completed
Write a generator-based context manager tag(name) with @contextmanager that prints <name> before the block and </name> after. Read a tag and a line of words and print each word inside the tag block.
Input
Two lines: tag, words.
Output
The wrapped output.
Example
Input
ul apple banana
Output
<ul> apple banana </ul>
Editor
Python
Hints
Code before yield runs on enter; code after it runs on exit.
Use try/finally so the closing tag always prints.
Solution explanation
@contextmanager turns a simple generator into a context manager — no class needed.
Show solution
from contextlib import contextmanager
@contextmanager
def tag(name):
print(f"<{name}>")
try:
yield
finally:
print(f"</{name}>")
name = input()
words = input().split()
with tag(name):
for w in words:
print(w)