[英]try and if - local variable referenced before assignment
I tried run this code but I have an error:我尝试运行此代码,但出现错误:
a4 local variable referenced before assignment
赋值前引用的 a4 局部变量
a1 = '/fr/store/a'
try:
a2 = '/fr/store/b/collection/3432'
a2_store = "store"
a3 = a2.split ("store/")
a4 = a3[0]
if a2_store in a2:
print ('a2')
except:
if a4 not in a1:
print ('a1')
If an exception is raised before a4 = a3[0]
is executed (for example, if a3[0]
raises an IndexError
), then a4
is, indeed, not defined.如果在执行
a4 = a3[0]
之前引发异常(例如,如果a3[0]
引发IndexError
),则确实没有定义a4
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.