LiteSpeed Support Forums

LiteSpeed Support Forums (http://www.litespeedtech.com/support/forum/index.php)
-   CGI/Perl/Python (http://www.litespeedtech.com/support/forum/forumdisplay.php?f=22)
-   -   Need help with this python script (http://www.litespeedtech.com/support/forum/showthread.php?t=2346)

astonmartin 09-30-2008 04:28 PM

Need help with this python script
 
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]



All times are GMT -7. The time now is 11:26 PM.