简体   繁体   中英

UnboundLocalError error: local variable 'i' referenced before assignment

I'm writing a program in selenium python. I pasted here part of the code from my program (I did not paste all the code because it has 800 lines) with the UnboundLocalError error: local variable 'i' referenced before assignment, the error occurs exactly at i += 1.

    global i
    i = 0
    odpowiadanieobserwowaniestronfb0()

def odpowiadanieobserwowaniestronfb0():
    if i > ileraz:
        driver.quit
        skonczono()
'''
    try:
        testt = driver.find_element_by_xpath('')
    except Exception:
        odpowiadanieobserwowaniestronfb1()
    zleskonczono1()
'''
def odpowiadanieobserwowaniestronfb1():
    i += 1

global keyword tells the function, not the whole module / file, what variables should be considered declared outside the scope of the said function. Try this:

def odpowiadanieobserwowaniestronfb1():
    global i
    i += 1

There are two options:

You can use your global variable:

def odpowiadanieobserwowaniestronfb1():
    global i
    i += 1

or you pass the i to the function:

def odpowiadanieobserwowaniestronfb1( i ):
    return i += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM