gpt4 book ai didi

python - 如何修复高阶函数来模拟联名银行账户?

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:38 25 4
gpt4 key购买 nike

Q.

Suppose that our banking system requires the ability to make joint accounts. Define a function make_joint that takes three arguments.

  1. A password-protected withdraw function,

  2. The password with which that withdraw function was defined, and

  3. 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 down 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 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_withdraw(balance, password):
"""Return a password-protected withdraw function.

>>> w = make_withdraw(100, 'hax0r')
>>> w(25, 'hax0r')
75
>>> w(90, 'hax0r')
'Insufficient funds'
>>> w(25, 'hwat')
'Incorrect password'
>>> w(25, 'hax0r')
50
>>> w(75, 'a')
'Incorrect password'
>>> w(10, 'hax0r')
40
>>> w(20, 'n00b')
'Incorrect password'
>>> w(10, 'hax0r')
"Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
>>> w(10, 'l33t')
"Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
"""
incorrect_password_list = []
count = 0
def withdraw(amount, input_passwd):
nonlocal count
if count < 3:
if input_passwd == password:
nonlocal balance
if amount < balance:
balance -= amount
return balance
else:
return "Insufficient funds"
else:
nonlocal incorrect_password_list
incorrect_password_list.append(input_passwd)
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_passwd, new_passwd):
"""
>>> 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'
"""
value = withdraw(0, old_passwd)
if type(value) == str:
return "Incorrect password"
else:
def joint_withdraw(balance, input_passwd):
if (input_passwd == old_passwd) or (input_passwd == new_passwd):
return withdraw(balance, old_passwd)
return joint_withdraw

我的问题:

除了上述解决方案 make_joint 的文档字符串中提到的测试用异常(exception),以下其他测试用例尚未通过,请提供修改 make_joint 的提示以满足这些给定的附加测试用例测试用例(如下)。

    >>> 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']"

最佳答案

让我们只关注这些行:

def joint_withdraw(balance, input_passwd):
if (input_passwd == old_passwd) or (input_passwd == new_passwd):
return withdraw(balance, old_passwd)

input_passwd既不是old_passwd也不是new_passwd时会发生什么?您的函数不执行任何操作,也没有给出任何响应。

请注意,您已经有了响应错误密码的代码。如何定义 joint_withdraw 来重用该功能?

按照我暗示的方式解决该问题,您应该能够让 j2(5, 'hax0r') 工作。

关于python - 如何修复高阶函数来模拟联名银行账户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com