LearnPy Hub
← Context Managers problems
Hard

All or Nothing

Write a context manager Transaction(accounts) that copies the balances on entry and, if an exception happens, restores them (and swallows it after printing rolled back). Apply transfers from to amount until END; a transfer that would make a balance negative raises ValueError. Each transfer is its own transaction. Print the final balances sorted by name.

Input

Lines 'name balance' until ---, then transfers until END.

Output

Messages, then balances.

Sample 1

Input
Mim 100
Rafi 50
---
Mim Rafi 30
Rafi Mim 500
END
Output
rolled back
Mim 70
Rafi 80

Sample 2

Input
A 0
B 0
---
A B 1
END
Output
rolled back
A 0
B 0
solution.py
Output appears here
All or Nothing · Python practice problem · LearnPy Hub