Need help with classes/inheritance

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

  1. maxiaojun

    maxiaojun

    Joined:
    Feb 7, 2011
    Messages:
    1
    Likes Received:
    0
    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. Advertisements

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. Nathon Dalton

    Need help with windows form inheritance

    Nathon Dalton, Oct 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    505
    Nathon Dalton
    Oct 17, 2003
  2. Hai Nguyen

    Inheritance Error!!! Need help so much

    Hai Nguyen, Jan 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    688
    Hai Nguyen
    Jan 12, 2004
  3. Greg Copeland

    inheritance with new-style classes - help

    Greg Copeland, May 6, 2005, in forum: Python
    Replies:
    3
    Views:
    366
    Terry Reedy
    May 6, 2005
  4. SpreadTooThin

    Need help with syntax on inheritance.

    SpreadTooThin, Oct 4, 2006, in forum: Python
    Replies:
    5
    Views:
    313
    Peter Otten
    Oct 4, 2006
  5. Fao
    Replies:
    13
    Views:
    585
    osmium
    May 2, 2006
  6. barbaros
    Replies:
    7
    Views:
    354
    barbaros
    Apr 24, 2008
  7. Immortal Nephi

    Two base classes need Inheritance?

    Immortal Nephi, Aug 18, 2008, in forum: C++
    Replies:
    2
    Views:
    380
    Chris M. Thomasson
    Aug 19, 2008
  8. dwightarmyofchampions@hotmail.com

    I need help understanding inheritance and virtual functions

    dwightarmyofchampions@hotmail.com, May 7, 2009, in forum: C++
    Replies:
    9
    Views:
    603
    Bart van Ingen Schenau
    May 11, 2009
Loading...