Medium
Undo a List on Error
Write a
@contextmanager backup(lst) that saves a copy of the list and, if an exception happens inside the block, puts the old contents back and swallows the error. Start with an empty list; for each input line (a batch of tokens), append every token converted to int inside with backup(nums):. Print the list at the end.Input
n, then n lines.
Output
A Python list.
Sample 1
Input
2 1 2 3 x 4
Output
[1, 2]
Sample 2
Input
1 a
Output
[]
Output appears here