from stacks import Stack

def postfix(expression):
    st = Stack()
    tokens = expression.split(" ")
    for piece in tokens:
        if piece in ["+","-","*","/"]:
            if len(st)>=2:
                right = st.pop()
                left = st.pop()
                if piece == "+":
                    st.push(left + right)
                elif piece == "-":
                    st.push(left - right)
                elif piece == "*":
                    st.push(left * right)
                else:
                    st.push(left / right)
            else:
                print "Error : The expression is not proper postfix"
                return
        else:
            st.push(int(piece))
    if len(st)==1:
        return st.pop()
    else:
        print "Error : The expression is not proper postfix"
