UnboundLocalError:赋值前引用的局部变量

时间:2014-09-29 14:19:07

标签: python python-2.7

waitTime=0.5

def is_ava():
    waitTime = waitTime + 0.1
    print waitTime

if __name__ == '__main__':
    is_ava()

为什么UnboundLocalError: local variable 'waitTime' referenced before assignment,我已经宣布了waitTime。

Python版本:2.7

1 个答案:

答案 0 :(得分:4)

要修改全局变量,您需要declare it inside the function as a global variable

waitTime=0.5

def is_ava():
    global waitTime  # <--------
    waitTime = waitTime + 0.1
    print waitTime

if __name__ == '__main__':
    is_ava()