UnboundLocalError: local variable referenced before assignment

Question:

waitTime=0.5

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

if __name__ == '__main__':
    is_ava()

Why UnboundLocalError: local variable 'waitTime' referenced before assignment , I have declared waitTime.

Python version: 2.7

Asked By: Gank

||

Answers:

To modify global variable, you need to 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()
Answered By: falsetru
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.