Need help with this python script

Discussion in 'CGI/Perl/Python' started by astonmartin, Oct 1, 2008.

  1. astonmartin

    astonmartin New Member

    OK I pretty new to python but I'm pretty sure this should work. everything is defined, only posted whats causing the problem. expression is a Reverse Polish Notation for a calculator (ex 1 2 +) and has already been parsed at this point into single array elements. All I get after running is a SERVER ERROR.

    Code:
    stack = []
    i = 0
    j = 0
    
    for i in range(len(expression))
    	if len(expression) <= 2
    		print "Invalid Expression: Not enough elements"
    		break
    	if expression[i] == '+' || expression[i] == '-' || expression[i] == '*'
    		if i == 0 || i == 1
    			print "Invalid Expression: Operator out of place!"
    			break
    		if expression[i] == '+'
    			stack[j-2] = stack[j-2] + stack[j-1]
    			j = j - 1
    			stack[j] = ""
    		if expression[i] == '-'
    			stack[j-2] = stack[j-2] - stack[j-1]
    			j = j - 1
    			stack[j] = ""
    		if expression[i] == '*'
    			stack[j-2] = stack[j-2] * stack[j-1]
    			j = j - 1
    			stack[j] = ""
    	else
    		stack[j] = int(expression[i])
    		j = j + 1
    
    print stack[0]

Share This Page