def c(m): s=m if m>0 else 1 def f(): nonlocal s s=s*2 return s for s in range(4): print(f(),end='') c(-1)
时间: 2024-05-19 16:12:20 浏览: 19
这段代码的输出结果是什么?
首先,函数 `c` 中传入的参数 `m` 会被赋值给变量 `s`,如果 `m` 大于 0,那么 `s` 等于 `m`,否则 `s` 等于 1。
然后定义了一个内部函数 `f`,这个函数中使用了 `nonlocal` 关键字将外层函数 `c` 中的变量 `s` 声明为非局部变量,这样在 `f` 中就可以修改 `c` 函数中的变量 `s` 的值了。`f` 函数的逻辑是将 `s` 的值乘以 2 并返回。
接下来使用了一个循环,将变量 `s` 分别赋值为 0、1、2、3,然后依次调用 `f` 函数并输出返回值,这里使用了 `end=''` 参数表示输出不换行。
最后调用函数 `c` 并传入参数 -1,这里由于 -1 小于等于 0,所以变量 `s` 被赋值为 1,然后循环输出了 `2 4 8 16`,因为 `f` 函数中每次都将 `s` 的值乘以 2。
相关问题
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 withdrawdef 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对此函数输入和输出如下>>> 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']"请修改
修改后的代码如下所示:
```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" or result == "Your account is locked. Attempts: ['{}']".format(old_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. 在 `make_joint` 函数中,增加了对原始账户密码错误或者已经被锁定的情况的处理。
2. 在返回结果时,增加了对账户被锁定的情况的处理,使得输出符合要求。
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
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.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)