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. If the password is incorrect or cannot be verified because the underlying account is locked, the make_joint should propagate the error. Otherwise, it 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.已知函数def make_withdraw(balance, password): num = 0 array = {} def withdraw(amount, input_password): nonlocal balance nonlocal password nonlocal num nonlocal array if num < 3: if input_password != password: num += 1 array[num - 1] = input_password return 'Incorrect password' else: if amount > balance: return 'Insufficient funds' balance = balance - amount else: a = "Your account is locked. Attempts: ['%s', '%s', '%s']" % (array[0], array[1], array[2]) return a return balance return withdraw
时间: 2024-02-14 18:06:54 浏览: 264
byf简单平衡_suppose6gx_byf112com_简单平衡_
Here is an implementation of the make_joint function:
```
def make_joint(withdraw, password, new_password):
# First, try to withdraw with the original password to make sure it's valid
result = withdraw(0, password)
if isinstance(result, str):
# The password was incorrect or the account is locked, so propagate the error
return result
# Create a new withdraw function that can be accessed with either password
def joint_withdraw(amount, input_password):
if input_password == new_password or input_password == password:
# The password is correct, so withdraw from the account
return withdraw(amount, password)
else:
# The password is incorrect, so propagate the error
return withdraw(amount, input_password)
# Return the new withdraw function
return joint_withdraw
```
The make_joint function takes three arguments: the original password-protected withdraw function, the password used to create that function, and the new password that can also access the original account. First, it tries to withdraw 0 from the account using the original password to make sure it's valid. If the password is incorrect or the account is locked, the function propagates the error.
Assuming the password is valid, the function creates a new withdraw function that can be accessed with either the original password or the new password. This new function checks which password was used and calls the original withdraw function with the original password. If an incorrect password is used, the function propagates the error.
Finally, the function returns the new withdraw function that can be used to access the account with either password.
阅读全文