LiteSpeed Technologies
Download Download     Blog Blog     Wiki Wiki     Forum Forum     Store     Contact Contact    

Go Back   LiteSpeed Support Forums > External Applications > CGI/Perl/Python > Need help with this python script

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 09-30-2008, 04:28 PM
astonmartin astonmartin is offline
New Member
 
Join Date: Sep 2008
Posts: 1
Default 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]
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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



- Archive - Top
© Copyright 2003-2011 LiteSpeed Technologies, Inc. All rights reserved. Privacy Policy.