I am having problems with my reverse polish calculator code. The code is as follows:

import operator

OPERATORS = {
        '+': operator.add,
        '-': operator.sub,
        '*': operator.mul,
        '/': operator.div,
        }

class Stack(object):
    "Creates a stack to enter numbers or operators into"

    def __init__(self):
        self.storage = [0,0,0,0]
        self.sptn = -1        

    def push(self, item):
        self.sptn = self.sptn + 1
        self.storage[self.sptn] = item
        
    def pop(self):
        self.sptn = self.sptn -1
        
    def top(self):        
        return self.storage[self.sptn]

    def __rep__(self):
        return self.storage[:self.sptn + 1]
    

def get_input():

    equation = raw_input("Enter the equation in reverse polish notation: ")
    return equation

def evaluate_rpn(equation):

    s = Stack()

    try:
        for item in equation.split():
            try:
                result = float(item)
                s.push(result)
            except ValueError:
                x = s.pop()
                y = s.pop()
                result = OPERATORS[item](x, y)
                s.push(result)
        result = s.top()
            
    except IndexError:
        print "Please input at least two numbers before entering an operator."

def main():
    equation = get_input()
    answer = evaluate_rpn(raw_input("Enter the equation in reverse polish notation: "))
    print answer

The code trips up when it hits an operator for some reason. The line is causing me trouble is the "results = OPERATOR[item] (x, y)"

I would appreaciate any help!!! Please!!!

Thanks

It's always good to use a few test prints:

import operator

OPERATORS = {
        '+': operator.add,
        '-': operator.sub,
        '*': operator.mul,
        '/': operator.div,
        }

class Stack(object):
    "Creates a stack to enter numbers or operators into"

    def __init__(self):
        self.storage = [0,0,0,0]
        self.sptn = -1

    def push(self, item):
        self.sptn = self.sptn + 1
        self.storage[self.sptn] = item
        
    def pop(self):
        self.sptn = self.sptn -1
        
    def top(self):
        return self.storage[self.sptn]

    def __rep__(self):
        return self.storage[:self.sptn + 1]
    

def get_input():

    equation = raw_input("Enter the equation in reverse polish notation: ")
    return equation

def evaluate_rpn(equation):
    
    s = Stack()

    try:
        for item in equation.split():
            try:
                result = float(item)
                s.push(result)
            except ValueError:
                x = s.pop()
                y = s.pop()
                print x, y   # test --> None None  problem is here!!!
                result = OPERATORS[item](x, y)
                s.push(result)
        result = s.top()
        print result  # test
        return result
            
    except IndexError:
        print "Please input at least two numbers before entering an operator."

def main():
    # use for testing
    equation = '2 3 +'
    #equation = get_input()
    answer = evaluate_rpn(equation)
    print answer

main()

The problem is in your class Stack. If you use a Python Queue as a stack it works fine:

import operator
import Queue

OPERATORS = {
        '+': operator.add,
        '-': operator.sub,
        '*': operator.mul,
        '/': operator.div,
        }

def get_input():

    equation = raw_input("Enter the equation in reverse polish notation: ")
    return equation

def evaluate_rpn(equation):
    
    # use a Python queue for a stack
    s = Queue.Queue(-1)

    try:
        for item in equation.split():
            try:
                result = float(item)
                s.put(result)
            except ValueError:
                x = s.get()
                y = s.get()
                #print x, y   # test
                result = OPERATORS[item](x, y)
                s.put(result)
        result = s.get()
        #print result  # test
        return result
            
    except IndexError:
        print "Please input at least two numbers before entering an operator."

def main():
    # use for testing
    equation = '2 3 +'
    # otherwise use ...
    #equation = get_input()
    answer = evaluate_rpn(equation)
    print answer  # '2 3 +' --> 5.0

main()

Edited 7 Years Ago by sneekula: code

Ok, thank you for the corrections. The problem is that I have to use a class for this part of the course. I thought I started the positions by s.sptn (start point) = -1 in the __init__(self) function. Any more help with trying to figure out why the class Stack is incorrect would be appreciated!

Thanks,
Linda

Why don't you simply separate your class Stack out and send it through some simple tests to figure out the errors?

Looks like pop() should return something.

Edited 7 Years Ago by vegaseat: n/a

This question has already been answered. Start a new discussion instead.