Where we work hard not to lose any of your money!
(At least...not on purpose.)
123
"$1.23"
Download: jmu_bank.py
Functions defined so far:
money()
open_account()
deposit()
withdraw()
Fix the bug in money()
money(1234500)
"$12,345.00"
dollars
cents
:,d
:02d
import random accounts = [] # account numbers balances = [] # money, in cents numbers = list(range(10000, 100000)) random.shuffle(numbers)
range
numbers
random.shuffle()
>>> open_account() Opening new account Opened account #81017 81017 >>> deposit(81017, 123.4) Depositing $123.40 into account #81017 New balance is $123.40 123.4 >>> withdraw(81017, 23.4) Withdrawing $23.40 from account #81017 New balance is $100.00 100.0
Define a test_bank() function:
test_bank()
Add these lines at the very end:
if __name__ == "__main__": test_bank()
Run with Thonny's debugger:
If you haven't already, enable View > Variables (for globals)
def deposit(acct, amount): amount = int(round(amount * 100)) index = accounts.index(acct) balances[index] += amount return balances[index] / 100
acct
amount
index
What does this code print?
def change(x): x = 456 x = 123 change(x) print(x)
View in Python Tutor
def change(scores): lowest = min(scores) scores.remove(lowest) scores = [95, 53, 88] change(scores) print(scores)
Global variables in jmu_bank.py
accounts
balances
def open_account(): acct = numbers.pop(0) accounts.append(acct) balances.append(0) return acct
Write a close_account(acct) function
close_account(acct)
Ex: Account 12345 has a balance of $6.78
def deposit(acct, amount): """Deposit money into an account. Args: acct (int): account number amount (float): money to deposit Returns: float: updated account balance """
"""
Args:
Returns:
help()
When in the shell, help() is always there!
The help() function shows the docstring
Works on modules, functions, keywords, etc.
Can also run help() without an argument!
Write a docstring for your close_account()
close_account()
Run !flake8 jmu_bank.py and fix any errors
!flake8 jmu_bank.py
Try calling help(close_account) in the shell
help(close_account)
If time permits, write this function:
def transfer(acct_from, acct_to, amount): """Transfer money from one account to another."""
acct_from
acct_to
True
flake8