0

I have 2 files. driver.py and stack.py. I would like to inherit the base class stack.py's 2 functions into driver.py, but I am having issues with actually creating the class (encounter attribute errors while running). The code is supposed to ask for user input until 'end' is received and the program should output all the input.

Please disregard the ill-formatting, I had issues including the 'import' lines in the snippets.

  • stack.py

class Stack:

def __init__(self):
    '''A new empty stack'''
    self.items = []

def push(self, o):
    '''Make o the new top item in this Stack.'''
    self.items.append(o)
    
def pop(self):
    '''Remove and return the top item.'''
    return self.items.pop()

def peek(self):
    '''Return the top item.'''
    return self.items[-1]

def isEmpty(self):
    '''Return whether this stack is empty.'''
    return self.items == []

def size(self):
    '''Return the number of items in this stack.'''
    return len(self.items)

class UpStack:


def __init__(self):
    '''A new empty stack'''
    self.stack = []

def push(self, o):
    '''Make o the new top item in this Stack.'''
    self.stack.insert(0, o)
    
def pop(self):
    '''Remove and return the top item.'''
    return self.stack.pop(0)

def peek(self):
    '''Return the top item.'''
    return self.stack[0]

def isEmpty(self):
    '''Return whether this stack is empty.'''
    return self.stack == []

def size(self):
    '''Return the number of items in this stack.'''
    return len(self.stack)

  • driver.py

from stack import *

if __name__ == '__main__':
#-----------------------------------Stack Code-----------------------------------

                    s = Stack()
                    s.push('Hello')
                    print(s.pop())

                    data = ""
                    while data.lower() != 'end':
                            data = input("Enter Command: ")
                            if data.lower() != 'end':
                                    s.push(data)

                    while not s.isEmpty():
                            print(s.pop())

    #-----------------------------------UpStack Code-----------------------------------

                    u = UpStack()
                    u.push('Hello')
                    print(u.pop())

                    data = ""
                    while data.lower() != 'end':
                            data = input("Enter Command: ")
                            if data.lower() != 'end':
                                    u.push(data)

                    while not u.isEmpty():
                            print(u.pop())

  • driver.py attempted class version

from stack import *

    class Test:
	#-----------------------------------Stack Code-----------------------------------
                def stacker(self):
                        s = Stack()
                        s.push('Hello')
                        print(s.pop())

                        data = ""
                        while data.lower() != 'end'
                        data = input("Enter Command: ")
                                if data.lower() != 'end':
                                        s.push(data)

                                while not s.is_empty():
                                        print(s.pop())

        #-----------------------------------UpStack Code-----------------------------------
                def upstacker(self):
                        u = UpStack()
                        u.push('Hello')
                        print(u.pop())

                        data = ""
                        while data.lower() != 'end':
                        data = input("Enter Command: ")
                                if data.lower() != 'end':
                                        u.push(data)

                                while not u.is_empty():
                                        print(u.pop())

Since I don't see anything when I actually run the code, I make an instance of it and this is what I get.

    >>> s = Stack()
    >>> s
    <stack.Stack object at 0x103bb3a58>
    >>> s.push(2)
    >>> s.stacker()
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        s.stacker()
    AttributeError: 'Stack' object has no attribute 'stacker'
2
  • can you post your error trace ? Commented Feb 4, 2015 at 15:05
  • when I run the code nothing shows up, so I make an instance of one of the classes. I will post in in the question. Commented Feb 4, 2015 at 15:35

1 Answer 1

1

That is because Stack() instance does not have stacker() method.

stacker method belongs to your Test class.

instead of typing

>>> s = Stack()
>>> s.stacker()

you should use

>>> t = Test()
>>> t.stacker()
Sign up to request clarification or add additional context in comments.

1 Comment

Alright. Sorry to seem slow but I would just stick that to the end of my driver.py code, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.