list.index在找不到元素时崩溃 [英] list.index crashes when the element is not found

查看:87
本文介绍了list.index在找不到元素时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么?


这很疯狂

WHAT ?

This is crazy

推荐答案

TkNeo写道:
TkNeo wrote:

什么?


这是疯狂的
WHAT ?

This is crazy



像狐狸一样疯狂? />

a = [1,2,3]

试试:

a.index(99)

除了:

a.append(99)

终于:

打印a.index(99)


我的问题:我应该抓住哪个例外?

**发自 http://www.teranews.com **

Crazy like a fox?

a = [1, 2, 3]
try:
a.index(99)
except:
a.append(99)
finally:
print a.index(99)

MY question: which exception should I actually be catching there?
** Posted from http://www.teranews.com **


En Fri,2008年5月2日15:25:13 -0300,TkNeo< ta*******@gmail.comescribió:
En Fri, 02 May 2008 15:25:13 -0300, TkNeo <ta*******@gmail.comescribió:

什么?


这是疯狂的
WHAT ?

This is crazy



崩溃?或者引发一个ValueError异常,这是完全正常的吗?


-

Gabriel Genellina

crashes? Or raises a ValueError exception, which is perfectly normal?

--
Gabriel Genellina

5月2日下午1:58,Nick J Chackowsky< mediocre_per ... @ hotmail.com>

写道:
On May 2, 1:58 pm, Nick J Chackowsky <mediocre_per...@hotmail.com>
wrote:

TkNeo写道:
TkNeo wrote:

什么?
WHAT ?


这是疯狂的
This is crazy



像狐狸一样疯狂?


a = [1,2,3]

试试:

a.index(99)

除外:

a.append(99)

终于:

打印a.index(99)


MY问题:我实际上应该抓住哪个例外?

**发布于:http://www.teranews.com**


Crazy like a fox?

a = [1, 2, 3]
try:
a.index(99)
except:
a.append(99)
finally:
print a.index(99)

MY question: which exception should I actually be catching there?
** Posted fromhttp://www.teranews.com**



ofcouse尝试捕获是可行的,但在理想上,索引函数

应该返回-1并且没有办法在地狱崩溃。

ofcouse try catch is going to work but in ideality the index function
should return a -1 and no way in hell crash.


这篇关于list.index在找不到元素时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆