Need help with classes/inheritance

Discussion in 'Python' started by maxiaojun, Feb 7, 2011.

  1. maxiaojun

    maxiaojun

    Joined:
    Feb 7, 2011
    Messages:
    1
    This is the error I am getting:
    Traceback (most recent call last):
    File "C:\Users\Ma\Desktop\CS\cs173\calc.py", line 19, in <module>
    main()
    File "C:\Users\Ma\Desktop\CS\cs173\calc.py", line 17, in main
    print(calc.top())
    File "C:\Users\Ma\Desktop\CS\cs173\RPN.py", line 36, in top
    return self._calcStack.top()
    AttributeError: Stack instance has no attribute '__len__'

    this is my code:
    import RPN

    def main():
    calc = RPN.RPNCalculator()
    RPNexpression = str(input("Enter RPN expression in string form and spaces between each number/operand: "))
    print(RPNexpression)
    RPNList = RPNexpression.split()
    print(RPNList)
    for i in RPNList:
    calc.push(i)
    print(calc.top())

    import stack

    class RPNCalculator(stack.Stack):

    def __init__(self):
    self._calcStack = stack.Stack()

    def push(self, operand):
    if operand == '+':
    self.add()
    elif operand == '-':
    self.subtract()
    elif operand == '*':
    self.multiply()
    elif operand == '/':
    self.divide()
    else:
    self._calcStack.push(int(operand))

    def top(self):
    return self._calcStack.top()

    def add(self):
    try:
    num2 = self._calcStack.pop()
    num1 = self._calcStack.pop()
    newSum = num1 + num2
    self._calcStack.push(newSum)
    except RuntimeError:
    pass

    def subtract(self):
    try:
    num2 = self._calcStack.pop()
    num1 = self._calcStack.pop()
    newSum = num1 - num2
    self._calcStack.push(newSum)
    except RuntimeError:
    pass

    def multiply(self):
    try:
    num2 = self._calcStack.pop()
    num1 = self._calcStack.pop()
    newSum = num1 * num2
    self._calcStack.push(newSum)
    except RuntimeError:
    pass

    def divide(self):
    if self._calcStack.top() == 0:
    raise ArithmeticError
    try:
    num2 = self._calcStack.pop()
    num1 = self._calcStack.pop()
    newSum = num1 / num2
    self._calcStack.push(newSum)
    except RuntimeError:
    pass


    class Stack:

    def __init__(self):
    self._stack = []

    def top(self):
    return self._stack[len(self._stack)-1]

    def push(self, item):
    self._stack.append(item)

    def pop(self):
    return self._stack.pop()

    def isEmpty(self):
    if len(self._stack) != 0:
    return False
    return True

    def __len__(self):
    return len(self._stack)

    These are all separate python files but i imported them correctly. I really am at a loss because the error seems to have nothing to do with the code the error is pointing to. Sorry about indenting I don't know how to preserve it in forum posts but I can 100% guarantee you indenting is correct.
     
    Last edited: Feb 7, 2011
    maxiaojun, Feb 7, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,179
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    842
    cppsks
    Oct 27, 2004
  3. Greg Copeland

    inheritance with new-style classes - help

    Greg Copeland, May 6, 2005, in forum: Python
    Replies:
    3
    Views:
    286
    Terry Reedy
    May 6, 2005
  4. barbaros
    Replies:
    7
    Views:
    269
    barbaros
    Apr 24, 2008
  5. Immortal Nephi

    Two base classes need Inheritance?

    Immortal Nephi, Aug 18, 2008, in forum: C++
    Replies:
    2
    Views:
    306
    Chris M. Thomasson
    Aug 19, 2008
Loading...

Share This Page