python中函数使用全局变量

python在使用全局变量时需要在函数内部先对变量加一个global

s = 0

def test():
    global s
    s +=2
    print(s)

if __name__ == "__main__":
    test()

猜你喜欢

转载自blog.csdn.net/oMoDao1/article/details/82773610