Where we work hard not to lose any of your money!
(At least...not on purpose.)
All money stored as integers (Ex: 123 cents) All input/output shown as floats (Ex: "$1.23")
Download: jmu_bank.py
Functions defined so far:
money()
open_account()
deposit()
withdraw()
Fix the bug in money()
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
Global variables in jmu_bank.py
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
accounts
balances
Hint: Look at the use of index in deposit()