x = -1
def one():
x = 0
def two():
x = 1
def three():
nonlocal x
print(x)
three()
two()
if __name__ == "__main__":
one()
求问x怎么跳过two函数获取one函数中x的值?感谢大神,不胜感激
nonlocal x 只能是找最近一层函数的x变量,要把two()中的x改成别的名称才行,否则没有办法跳过two()中的x。
x = -1
def one():
x = 0
def two():
y = 1
def three():
nonlocal x
print(x)
three()
two()
if __name__ == "__main__":
one()
最简单的方法就是传参啊,你把one中的x传入tow中就ok了啊。
def two(x):
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632