Python:使用if语句中声明的变量

时间:2018-04-08 15:20:52

标签: python

for case in log:
    for event in case:
        if event in process_model:
            index = process_model.index(event)
        else: print(event, index)
---------- output-----------
NameError: name 'index' is not defined

我仍然需要使用索引

1 个答案:

答案 0 :(得分:1)

这是你要找的吗?

for case in log:
    for event in case:
        if event in process_model:
            index = process_model.index(event)
        else:
            index = None
        print(event, index)

或者(如果情况有__repr__方法并且您希望获得充分信息):

for case in log:
    for event in case:
        if event in process_model:
            index = process_model.index(event)
        else:
            index = None
        print(case, event, index)