185. Python Program to Make a Bank Account Class
MediumMethods
Overview
Methods that change an attribute, and a method that protects its object by raising an error.
Problem Statement
Define a class BankAccount with an owner and a balance, and two methods:
deposit(amount)adds the amount to the balance.withdraw(amount)takes the amount out. If the amount is more than the balance, it raises aValueErrorand changes nothing.
Read the owner's name, the starting balance, then the number of commands, then one command per line: deposit 200 or withdraw 100 (whole numbers). Run each command on the account. When a withdrawal fails, catch the error and print a line like Cannot withdraw 1000: not enough money. At the end, print the balance like this: Sunny's balance: 650.
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
The owner, the starting balance, the number of commands, then the commands. Example:
Sunny
500
4
deposit 200
withdraw 100
withdraw 1000
deposit 50
Output
The output for that example:
Cannot withdraw 1000: not enough money
Sunny's balance: 650
The tests try 4 different inputs.