我试图通过从数据库获得的结果进行迭代,如果条件正在检查条件,如果它是真的,那么我想增加值
for row in rows:
if row.LeavesApplied.type_id == "SICK_LEAVE" and row.Employee.leave_eligibility_id == 2:
global slcount
slcount+= 1
elif row.LeavesApplied.type_id == "CASUAL_LEAVE" and row.Employee.leave_eligibility_id == 2:
clcount += 1
elif row.LeavesApplied.type_id == "PRIVILEGED_LEAVE" and row.Employee.leave_eligibility_id == 2:
plcount += 1
但出现错误提示
UnboundLocalError: local variable 'slcount' referenced before assignment
我也尝试通过提供“全局”,但它显示以下错误。
NameError: global name 'slcount' is not defined
plcount
在使用它之前,您没有为它分配值。
slcount = clcount = plcount = 0
for row in rows:
...
多一点点:
Python是动态类型的语言
这意味着您可以在Python中执行此操作:
> foo = 12
> foo = 'aaa'
你看?我可以将任何类型的值分配给变量。而不是使用静态类型语言:
> int foo;
> foo = 12;
> foo = 'aaa'; // you can't do this!
但是,这并不意味着您可以在Python中执行此操作:
> foo = foo + 1 # you haven't assign a value to foo!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句