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的值?感谢大神,不胜感激
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()