157. Python Program to Withdraw Money with raise
Mediumraise
Overview
A function that refuses bad values with raise, and a caller that explains them.
Problem Statement
The account has a balance of 1000. Write a function withdraw(balance, amount) that returns the new balance, but raises a ValueError:
- with the message
Amount must be more than 0if the amount is 0 or less - with the message
Not enough moneyif the amount is more than the balance
Read the amount (a whole number) and call withdraw(1000, amount). Print the result like this for 300:
Balance left: 700
If withdraw() raises an error, print Error: and its message, like Error: Not enough money. If the input is not a whole number at all, print Please type a whole number.
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
One value. Example:
300
Output
The output for that example:
Balance left: 700
The tests try 5 different inputs.