【发布时间】:2014-09-11 18:10:24
【问题描述】:
我的一个程序应该使用以下代码抛出 IndexError:
x = "Hello"
y = x[6]
但是,它返回的不是这个,而是 SegmentationFault:11。这是我在 python 2.7.2 中运行它的时候。
当我在 python 3.4.1 中尝试它时,它抛出了IndexError。为什么会发生这种情况,有没有办法解决这个问题?谢谢!
编辑: 这是我跑的:
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "Hello"
>>> x[6]
Segmentation fault: 11
而在 python 3.4.1 中,
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "hello"
>>> x[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
【问题讨论】:
-
你能发布错误的完整输出吗?
-
不只是说“类似这样的代码”,你能产生minimal example code that actually causes a segmentation fault when run吗?否则,我们只能胡乱猜测。
-
这实际上是我运行的确切代码。我只是在学习尝试...除了语法。我也会编辑错误的帖子。
-
那些是从实际的口译员会话中复制粘贴(未重新输入,未修改)的吗?如果是这样,请尝试将您的 2.7.2 安装更新到 Python 2.7.8。这种段错误不应该发生。
-
我无法复制它here。也许它特定于早期版本或 OSX 版本?这个是一个月后的(并且不在 OS X 上运行)