# purpose # - write function # - local and global --> accumulate # # activity: # - Create a python module (i.e., python file) # that has multiple functions to maintain balance in # a checking account and a saving account # and; and to retrieve the balance information. # - Let's say, there are 4 functions: # -- deposit(amt, to_which_acct) # -- withdraw(amt, from_which_acct) # -- transfer(amt, from_which_acct, to_which_acct) # -- get_total_fund() --> return avai_checking + avai_saving # - hint: use global to keep track of available balance # in checking and saving, avai_checking, avai_saving # - to test your functions, # write test cases to call these functions # -- in addition to happy paths, always consider edge cases # (later the semester, we'll discuss how to handle # exception; therefore, we'll leave exception cases for now) def deposit(amt, to_which_acct): """ Add amount to given account :param amt: :param to_which_acct: :return: """ global saving global checking if to_which_acct == "saving": saving += float(amt) if to_which_acct == "checking": checking += float(amt) def withdraw(amt, from_which_acct): """ Withdraw amount from the specified account :param amt: :param from_which_acct: :return: """ global saving global checking if from_which_acct == "saving": saving -= float(amt) if from_which_acct == "checking": checking -= float(amt) def transfer(amt, from_which_acct, to_which_acct): """ Transfer amount from from_which_acct to to_which_acct note: normally, banking transaction requires user to specify "from" and "to" account. However, for this exercise and for simplicity, since there are only 2 accounts and transferring from saving to saving does not make sense, we only check which is the "from" account. :param amt: :param from_which_acct: :param to_which_acct: :return: """ global saving, checking # multiple variables can be written in the same line if from_which_acct == "saving": saving -= amt checking += amt elif from_which_acct == "checking": checking -= amt saving += amt # def get_total_fund(): # # """ # return the sum of saving and checking # :param: # :param: # :return: total_fund # """ # # global saving # not needed as we don't expect to update it # # global checking # not needed as we don't expect to update it # # return saving + checking # what if instead we want get_total_fund() to simply print # the total fund, let's modify the function def get_total_fund(): """ return the sum of saving and checking :param: :param: :return: total_fund """ # global saving # not needed as we don't expect to update it # global checking # not needed as we don't expect to update it print('in get_total_fund() :', saving + checking) saving = 0 checking = 0 deposit(20, "saving") print('1. deposit 20 to saving, saving =', saving) deposit(25, "checking") print('2. deposit 25 to checking, checking =', checking) withdraw(10, "saving") print('3. withdraw 10 from saving, saving =', saving) withdraw(5, "saving") print('4. withdraw 5 from saving, saving =', saving) withdraw(5, "checking") print('5. withdraw 5 from checking, checking =', checking) deposit(10, "saving") print('6. deposit 10 to saving, saving =', saving) # print(get_total_fund()) get_total_fund() transfer(10, "saving", "checking") print('7. transfer 10 from saving to checking: balance saving, checking =', saving, checking)