Exception Python不捕获ValueError异常

Exception Python不捕获ValueError异常,exception,indexing,try-catch,Exception,Indexing,Try Catch,运行代码时,Python不会捕获ValueError异常: trip_titles = ['Split Duty (stand-ups)','Locals','Two Days','Three Days','Four Days','Five Days'] # loop through each row and parse trip preference for index, entry in survey.iterrows(): trip_prefs = entry[13].spli

运行代码时,Python不会捕获ValueError异常:

trip_titles = ['Split Duty (stand-ups)','Locals','Two Days','Three Days','Four Days','Five Days']


# loop through each row and parse trip preference
for index, entry in survey.iterrows():
    trip_prefs = entry[13].split("|")
    missing = list(range(len(trip_prefs),6))
    for i in missing:
        trip_prefs.append(0)
    # pad less than 6 choices
    for i in list(range(1,7)):
        survey.loc[index,'Pref'+str(i)] = trip_prefs[i-1]
    for ititle in trip_titles:
        try:
            score = trip_prefs.index(ititle)            
        except ValueError:
            score = 0
这是我得到的错误:

trip_prefs.index(ititle)
*** ValueError: 'Locals' is not in list
当我复制stackoverflow的代码行时,它工作了

trip_titles = ['Split Duty (stand-ups)','Locals','Two Days','Three Days','Four Days','Five Days']

trip_prefs = ['Split Duty (stand-ups)', 'Two Days', 'Three Days', 0, 0, 0]

for ititle in trip_titles:
    try:
        score = trip_prefs.index(ititle)            
    except ValueError:
        score = 0
    print(ititle+' '+str(score))
结果:

!runcell(0, 'C:/Users/Dan/Documents/Projects/CloudStation/untitled0.py')
Split Duty (stand-ups) 0
Locals 0
Two Days 1
Three Days 2
Four Days 0
Five Days 0
为什么原始代码(列在顶部)没有捕获异常,而摘录的代码却捕获异常