…
…
Write deposit, withdraw, and transfer functions with their rules; raise errors, check them with assert tests, and save the transaction log to a CSV file.
In this project you will write a small bank program with three accounts. Deposit, withdraw, and transfer transactions must follow the rules: a negative amount, an account that does not exist, an insufficient balance, or a transfer to the same account is rejected. Every successful transaction is written to a log, and at the end the log is saved to a CSV file.
This is the project of the Final project lesson (40); as in lesson 39, first plan the request and the function list. The minimum requirement: at least 3 functions and at least 1 assert test for each of them.
Lessons used: functions (14–16), tests with assert (19), dictionaries and tuples (26–29), sorting with lambda (31), try, except, raise (32), CSV files (34), turning a problem into a program and finishing patterns (39–40).
reset() returns the accounts to their starting balances. Every cell starts its work with reset(); this way you get the same result however many times you run the cell.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The accounts are kept in a dictionary: the key is the account number, and the value is a dictionary that holds the owner and the balance. Successful transactions will be added to the log list.
reset() clears the existing dictionary and list in place and loads the starting values; it does not create a new dictionary, so global is not needed.
find_account(number) must return the account if it exists, and raise a ValueError if it does not. You check whether a key is in a dictionary with in.
This step's code cell is for members.Sign up free
check_amount(amount) rejects an amount that is zero or negative. deposit(number, amount) must first check the amount and the account, then increase the balance, add the tuple ("deposit", number, amount) to the log, and return the new balance.
The checks are made before the change: if a rule is broken, the error is raised and nothing has changed.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
This step's code cell is for members.Sign up free
withdraw(number, amount) is like deposit, but there is one more rule: you cannot withdraw more than the balance. In that case, raise a ValueError whose message looks like Insufficient balance: $12.50 available.
A successful withdrawal is written to the log as ("withdrawal", number, amount) and the new balance is returned.
This step's code cell is for members.Sign up free
transfer(source, target, amount) moves money from one account to another. The rules:
1. You cannot transfer to the same account.
2. The amount must be positive.
3. Both accounts must exist.
4. The source must have enough balance.
All the checks must be made before touching a balance. If money is taken from the source while the target account does not exist, the money is lost on the way. So first find both accounts, then change the balances.
This step's code cell is for members.Sign up free
Instead of trying all the rules by hand every time you change your code, you will write a test function. assert condition, "message" raises an AssertionError if the condition is false.
The ready-made tests try a deposit, a withdrawal, and a withdrawal from an empty account. The missing one is the transfer test: transfer $25.00 from AC-1001 to AC-1002, then check the balance of both accounts. A test that checks only the source cannot catch a bug that forgets to add money to the target. The check stage will run your test with a broken transfer and see whether it catches the bug.
This step's code cell is for members.Sign up free
At the end of the day, six transaction requests came to the bank; some of them break the rules. Try all of them in order: count the successful ones and print the reason for the rejected ones. Rejecting one transaction must not stop the program.
Each transaction is a tuple: the first item is the type ("deposit", "withdraw", "transfer"), and the rest are the arguments.
This step's code cell is for members.Sign up free
The transactions of the previous step are in the log. Write the log to the file log.csv with csv.writer (the first row is the header), then read the file back and print how many transactions were recorded.
Then sort the accounts by balance from largest to smallest and print an aligned summary. You give sorted the sort key as a function: key=lambda x: x[1]["balance"] looks at the balance of each (number, account) pair; reverse=True turns the order from largest to smallest.
This step's code cell is for members.Sign up free