Objective
Create a Python program that nests several with blocks one inside another and shows each opening and closing with an indentation that depends on the nesting level.
Instructions
- Create the class
Level, a context manager with a class attributedepththat starts at 0 and counts how many blocks are open. - On entering, show
Opens namewith 2 spaces of indentation for each level already open, and increase the depth. On leaving, decrease the depth and showCloses namewith the indentation of the current level. - Read an integer
nand thennnames. Create a recursive function that opens onewithper name, one inside another, and in the deepest one showsDepth: kwith the corresponding indentation.
3
a
b
c
Opens a
Opens b
Opens c
Depth: 3
Closes c
Closes b
Closes a
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise