[Tutor] Out of range
Gregor Lingl
glingl@aon.at
Tue Feb 4 08:38:02 2003
Adam Vardy schrieb:
>Someone have any suggestions on this index error?
>
>IndexError: string index out of range
>SyntaxError: invalid syntax
>
>
>>>>print index
>>>>
>>>>
>Traceback (most recent call last):
> File "<pyshell#75>", line 1, in ?
> print index
>NameError: name 'index' is not defined
>
>
>
>
>
You report us three Errors. The last one is clear (for me),
as there is no name index in Python (unless you define it).
In Python index is a method either of string or of list objects:
>>> "abcde".index("c")
2
>>> [1,2,3,4,5].index(4)
3
What are the situations, where the other two occur?
The first on is easy to imagine:
>>> a = "hey!"
>>> a[4]
Traceback (most recent call last):
File "<pyshell#10>", line 1, in ?
a[4]
IndexError: string index out of range
This occurs because ...
>>> for c in a:
c, a.index(c)
('h', 0)
('e', 1)
('y', 2)
('!', 3)
>>>
There is no character with index 4 in "hey!"
Only from knowing that a SyntaxError occured,
it's impossible to imagine what it possibly caused ...
Regards, Gregor