简体   繁体   English

UnboundLocalError 错误:赋值前引用了局部变量“i”

[英]UnboundLocalError error: local variable 'i' referenced before assignment

I'm writing a program in selenium python.我正在用 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.我在这里粘贴了我的程序中的部分代码(我没有粘贴所有代码,因为它有 800 行)和 UnboundLocalError 错误:在赋值之前引用了局部变量 'i',错误恰好发生在 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. global关键字告诉函数,而不是整个模块/文件,应该考虑在所述函数的范围之外声明哪些变量。 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:或者您将 i 传递给函数:

def odpowiadanieobserwowaniestronfb1( i ):
    return i += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:赋值前引用的局部变量'error' - UnboundLocalError: local variable 'error' referenced before assignment UnboundLocalError: 赋值前引用的局部变量错误 - UnboundLocalError: local variable referenced before assignment error UnboundLocalError:赋值前引用了局部变量“i” - UnboundLocalError: local variable 'i' referenced before assignment 赋值之前引用了unboundlocalerror局部变量'i' - unboundlocalerror local variable 'i' referenced before assignment 我收到错误消息:UnboundLocalError:分配前引用了本地变量'porc' - I got error : UnboundLocalError: local variable 'porc' referenced before assignment 错误-UnboundLocalError:分配前已引用本地变量“ VARIABLE_NAME” - Error - UnboundLocalError: local variable 'VARIABLE_NAME' referenced before assignment Fileconveyor错误-UnboundLocalError:分配前已引用局部变量'classname' - Fileconveyor Error - UnboundLocalError: local variable 'classname' referenced before assignment Flask 分页:UnboundLocalError:在赋值错误之前引用了局部变量“res” - Flask Pagination : UnboundLocalError: local variable 'res' referenced before assignment error 装饰器运行错误:“UnboundLocalError:赋值前引用了局部变量‘count’” - Decorator running error: "UnboundLocalError: local variable 'count' referenced before assignment" Python 错误:UnboundLocalError:赋值前引用了局部变量“score1” - Python error: UnboundLocalError: local variable 'score1' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM