This question already has answers here:

Don't understand why UnboundLocalError occurs [duplicate]
(8个答案)
waitTime=0.5

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

if __name__ == '__main__':
    is_ava()

为什么,我已经声明了等待时间。
python版本:2.7

最佳答案

要修改全局变量,需要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()

10-06 14:01