Custom Exception
Completed
Create class InsufficientFunds(Exception). Write withdraw(balance, amount) that raises it with the message need X more when the amount exceeds the balance. Start with balance 100, read withdrawals and print the new balance or the error.
Input
Line 1: n. Then n amounts.
Output
n lines.
Example
Input
3 30 90 70
Output
70 need 20 more 0
Editor
Python
Hints
A custom exception is a class that inherits from Exception.
Solution explanation
Custom exception types let callers catch exactly the failures that belong to your code.
Show solution
class InsufficientFunds(Exception):
pass
def withdraw(balance, amount):
if amount > balance:
raise InsufficientFunds(f"need {amount - balance} more")
return balance - amount
balance = 100
for _ in range(int(input())):
try:
balance = withdraw(balance, int(input()))
print(balance)
except InsufficientFunds as err:
print(err)