def calc(expr): "Processes an expression written in Polish notation from left to right" # Create Stack objects for the operators and operands operatorStack = Stack() operandStack = Stack() # Split expression into a list (temporary) expr = expr.split() for token in expr: # Check if the token is an operator if token in ["+", "-", "*", "/"]: operatorStack.push(token) pendingOperand = False # Check if the operand is a valid number elif is_number(token): operand = token # Check if a calculation should be done if pendingOperand == True: while operandStack.height() != 0: operand_1 = operandStack.pop() operator = operatorStack.pop() # print("Evaluating " + str(operand_1) + " " + operator + " " + str(operand)) operand = evaluate(operator, operand_1, operand) # Push the number to the operand stack operandStack.push(operand) pendingOperand = True else: # Default if the token is not a operator or cannot be a float print(str(token) + " is not a valid token.") # Return result return operandStack.pop()