[英]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.