Why do I get this error?

Rainy sill at optonline.net
Sat May 19 09:51:27 CEST 2001


On Sat, 19 May 2001 01:14:46 GMT, the_2nd_coming <jpetzold at twmi.rr.com> 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
> 
> hear is the source:
> 
>         import string
> 
>         def evaluate(list):
>                 num1 = list[0]
>                 num2 = list[2]
>                 operator = list[1]
>         
>                 if operator == '+':
>                         answer = num1 + num2
>                 elif operator == '-':
>                         answer = num1 - num2
>                 elif operator == 'X' or 'x' or '*':
>                         answer = num1*num2
>                 elif operator == '/':
>                         answer = num1 / num2
>                 else:
>                         print 'invalid operator'
>                 format = "the answer to %d%c%d is %d" % (num1, operator, num2, answer)
>         
>                 return format
> 
>         def fix_expression(list):
>                 l = []
>                 n1 = list[0]
>                 n2 = list[1]
>                 n3 = list[2]
>                 l.append(n1)
>                 l.append(n2)
>                 l.append(n3)
>                 evaluate(l)
>                 return format #needed to send format to main program from evaluate()
> # - - - - - - - - - - - - - - - -  - - - - - - - - - - - - - - - - - - - - 
> - - - - - - - - - - - - - - - - - - - - - 
>         expression = raw_input('please enter your singel operator expression')
>         string.strip(expression) # get rid of all leading and trailing white space#
>         list = string.split(expression)
> 
>         if len(list) >1:
>                 evaluate(list)
                    ^
Aside from things other people noted, change that to print 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

This will give an error because there is no variable 'format' in global scope,
so you should delete that line. 

> 
> 
> 
> why is it telling me the index is out of range?
> 
> thanks for your help
> 
> Jeremy


-- 
Jupiter and Saturn Oberon Miranda
And Titania Neptune Titan
Stars can frighten
        - Syd



More information about the Python-list mailing list