Objective
Create a Python program with a context manager that decides according to the type of error: the parameters of __exit__ tell whether there was an exception and of which class.
Instructions
- Create the class
Logwith a list of notes, anote(text)method that adds a text, and the__enter__(returns the object itself) and__exit__methods. - On exit, show
Log OK: k entriesif there was no error, orLog with error (Type): k entriesif there was (the name of the error class iskind.__name__), and then each note as- text. __exit__must returnTrueonly when the error isZeroDivisionError(that suppresses it). Any other error would propagate.- Read an integer
nand thennlines with two integersxandy. Inside the block note"x / y"and then"= result"(integer division). When finished, showEnd of the program.
3
6 3
1 0
8 2
Log with error (ZeroDivisionError): 3 entries
- 6 / 3
- = 2
- 1 / 0
End of the program
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise