UnboundLocalError: local variable 'url_request' referenced before assignment

python

Solution

You are assigning to a global variable, which means you need to mark it as a global:

def somefunction():
    global url_request
    url_request+=1

When you assign to a variable in a local scope, it is assumed to be a local variable unless you use a `global` statement to tell python otherwise first.

Problem

Think I'm going nuts here. ``` url_request = 0 def somefunction(): url_request+=1 if __name__ =='__main__': somefunction() ``` Gives me the UnboundLocalError. What important concept am I missing here?

Original source

Related problems