Refuse a Bad Account

Python for AIMedium+10 XP+10

Refuse a Bad Account

A bank Account should refuse to exist if its details make no sense.

Part 1: Account(owner, balance=0)

  • If owner is empty or only spaces, raise ValueError("owner needs a name").
  • Otherwise, if balance is below 0, raise ValueError("balance can't be negative").
  • Check the name first.
  • Store owner with spaces trimmed off its ends, and store balance.

Part 2: open_account(owner, balance=0)

Try to make an Account. Then:

  • if it worked, return "<owner> opened with <balance>", built from the account's attributes,
  • if a ValueError happened, return "refused: " followed by the error's message.

Examples

owner = ' Zoe '
balance = 15
returns 'Zoe opened with 15'
owner = 'Zoe'
balance = -1
returns "refused: balance can't be negative"
owner = "Ana"
balance = 50
returns 'Ana opened with 50'
owner = "Ben"
returns 'Ben opened with 0'
owner = "Cy"
balance = -5
returns "refused: balance can't be negative"

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run