Q8: Joint Account
Suppose that our banking system requires the ability to make joint accounts. Define a function make_joint that takes three arguments.
A password-protected withdraw function,
The password with which that withdraw function was defined, and
A new password that can also access the original account.
The make_joint function returns a withdraw function that provides additional access to the original account using either the new or old password. Both functions draw from the same balance. Incorrect passwords provided to either function will be stored and cause the functions to be locked after three wrong attempts.
Hint: The solution is short (less than 10 lines) and contains no string literals! The key is to call withdraw with the right password and amount, then interpret the result. You may assume that all failed attempts to withdraw will return some string (for incorrect passwords, locked accounts, or insufficient funds), while successful withdrawals will return a number.
Use type(value) == str to test if some value is a string:
|
def make_joint(withdraw, old_password, new_password): """Return a password-protected withdraw function that has joint access to the balance of withdraw. >>> w = make_withdraw(100, 'hax0r') >>> w(25, 'hax0r') 75 >>> make_joint(w, 'my', 'secret') 'Incorrect password' >>> j = make_joint(w, 'hax0r', 'secret') >>> w(25, 'secret') 'Incorrect password' >>> j(25, 'secret') 50 >>> j(25, 'hax0r') 25 >>> j(100, 'secret') 'Insufficient funds' >>> j2 = make_joint(j, 'secret', 'code') >>> j2(5, 'code') 20 >>> j2(5, 'secret') 15 >>> j2(5, 'hax0r') 10 >>> j2(25, 'password') 'Incorrect password' >>> j2(5, 'secret') "Your account is locked. Attempts: ['my', 'secret', 'password']" >>> j(5, 'secret') "Your account is locked. Attempts: ['my', 'secret', 'password']" >>> w(5, 'hax0r') "Your account is locked. Attempts: ['my', 'secret', 'password']" >>> make_joint(w, 'hax0r', 'hello') "Your account is locked. Attempts: ['my', 'secret', 'password']" """ "*** YOUR CODE HERE ***" |
use Python
Here's the python code attached of the banking system query:
def make_withdraw(bal, pwd): incorrect_password_list = [] count = 0 def withdraw(amt, input_pwd): nonlocal count if count < 3: if input_pwd == pwd: nonlocal bal if amt < bal: bal -= amt return bal else: return "Insufficient funds" else: nonlocal incorrect_password_list incorrect_password_list.append(input_pwd) count += 1 return "Incorrect password" else: return "Your account is locked. Attempts: ['{0}', '{1}', '{2}']".format(incorrect_password_list[0], incorrect_password_list[1], incorrect_password_list[2]) return withdraw def make_joint(withdraw, old_pwd, new_pwd): val = withdraw(0, old_pwd) if type(val) == str: return "Incorrect password" else: def joint_withdraw(bal, input_pwd): if (input_pwd == old_pwd) or (input_pwd == new_pwd): return withdraw(bal, old_pwd) else: return withdraw return joint_withdraw
WRITTEN CODE:
def make_withdraw(bal, pwd):
incorrect_password_list = []
count = 0
def withdraw(amt, input_pwd):
nonlocal count
if count < 3:
if input_pwd == pwd:
nonlocal bal
if amt < bal:
bal -= amt
return bal
else:
return "Insufficient funds"
else:
nonlocal incorrect_password_list
incorrect_password_list.append(input_pwd)
count += 1
return "Incorrect password"
else:
return "Your account is locked. Attempts: ['{0}', '{1}',
'{2}']".format(incorrect_password_list[0],
incorrect_password_list[1], incorrect_password_list[2])
return withdraw
def make_joint(withdraw, old_pwd, new_pwd):
val = withdraw(0, old_pwd)
if type(val) == str:
return "Incorrect password"
else:
def joint_withdraw(bal, input_pwd):
if (input_pwd == old_pwd) or (input_pwd == new_pwd):
return withdraw(bal, old_pwd)
else:
return withdraw
return joint_withdraw
-----------------------------------------------------------------------------------------------------------
Note: bal means balance, pwd means password, amt means amount.
Gives a big fat thumbs up, if you like the answer.
Thank you
Happy Chegging!
Q8: Joint Account Suppose that our banking system requires the ability to make joint accounts. Define...