livepython.net

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 0 if the amount is 0 or less
  • with the message Not enough money if 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.

Learn this firstChapter 17 · Python raise: Raise Your Own Exceptions
solution.py
# write your code here

EXPECTED OUTPUT

1Balance left: 700

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.