Python UnboundLocalError: local variable ‘xxxx’ referenced before assignment

理想

a = 0


def hello():
    a += 1
    print(a)


hello()

现实

UnboundLocalError: local variable 'a' referenced before assignment

解决

声明全局变量global

global a
a = 0


def hello():
    global a
    a += 1
    print(a)


hello()

不出意外的话不会报错了,如有意外请拨打114。

参考:

https://www.cnblogs.com/komean/p/11209780.html

https://www.jianshu.com/p/ba7ded1ecbe4

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注




Enter Captcha Here :