Why do I get this error?

the_2nd_coming jpetzold at twmi.rr.com
Fri May 18 21:14:46 EDT 2001


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)
        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?

thanks for your help

Jeremy



More information about the Python-list mailing list