Why do I get this error?

john davis john.davis at Sun.COM
Sun May 20 22:45:19 CEST 2001


the_2nd_coming wrote:
> 
> hello,
> 
> I am writing a program and the interpreter give me an error of:
> 
> Traceback (most recent call last):
>     File "mycalc.py", line 41, in ?
>         fix_expression(list)
>     File "mycalc.py", line 26, in fix_expression
>         n2 = list[1]
> IndexError: list index out of range
> 
>         if len(list) >1:
>                 evaluate(list)
>         elif len(list) == 1:
>                 fix_expression(list)
>         else:
>                 print 'um, you were spose to enter somthing dumbass'
> 
>         print format #found in the evaluate function
> 
> why is it telling me the index is out of range?

Because if the length of the list is 1, then the only valid index is 0. 
the list passed to fix_expression is guaranteed to have a length of 1,
so list[1] will always be invalid.

If I understand your code, you probably want list[0][1].

-- 
-----------------------------------------------------
john davis 858-626-8069 x58069 john.davis at west.sun.com



More information about the Python-list mailing list