上面的函数输入和输出样例如下>>> 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']"请修改
时间: 2023-08-15 09:07:12 浏览: 53
这段代码使其符合以下要求:
1. 当账户被锁定时,不能进行任何操作。
2. 每次输入错误密码时,应该记录该尝试并在账户锁定时显示所有尝试的密码。
修改后的代码:
```python
def make_withdraw(balance, password):
attempts = []
def withdraw(amount, pwd):
nonlocal balance
nonlocal password
nonlocal attempts
if len(attempts) >= 3:
return "Your account is locked. Attempts: " + str(attempts)
if pwd != password:
attempts.append(pwd)
return "Incorrect password"
if amount > balance:
return "Insufficient funds"
balance = balance - amount
return balance
return withdraw
def make_joint(withdraw, old_password, new_password):
attempts = []
result = withdraw(0, old_password)
if result == "Incorrect password":
return result
def joint(amount, pwd):
nonlocal attempts
if len(attempts) >= 3:
return "Your account is locked. Attempts: " + str(attempts)
if pwd == old_password or pwd == new_password:
return withdraw(amount, old_password)
else:
attempts.append(pwd)
return withdraw(amount, pwd)
return joint
```
上面的代码实现了以下功能:
1. 如果尝试次数超过3次,则账户被锁定,并返回所有尝试的密码。
2. 对于每个失败的密码尝试,都会记录它并在账户被锁定时显示所有尝试的密码。
3. 创建一个新的联合账户时,需要提供旧密码和新密码,并且只有使用这两个密码之一才能访问该账户。
阅读全文