0

I'm an absolute beginner with Python and generally not very good at coding yet, so hopefully my code isn't too laughable :D

I need to write a Stack Program, where the Stack & the Stack Program is a list. This is my code:

class Stack:
    def __init__(self):
        self.items = []

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

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

    def isEmpty(self):
        return self.items == []

def execute(list):
    result = Stack()
    for x in list:
        if 'LOAD' in x:
            number = substring_after(x, " ")
            result.push(number)
        elif 'ADD' in x:
            result = result.pop() + result.pop()
        elif 'MUL' in x:
            result = result.pop() * result.pop()
        elif 'SUB' in x:
            first = result.pop()
            result = result.pop() - first
        elif 'DIV' in x:
            first = result.pop()
            result = result.pop() / first
        elif 'PRINT' in x:
            print(result.pop())
            if result.isEmpty():
                print('Execution completed')
            else:
                print('There are still values in the Stack')
        else:
            print('Not a valid statement for Stack Program')

def substring_after(s, delim):
    return s.partition(delim)[2]

def main():
    operation1 = ["LOAD 2", "LOAD 3", "ADD", "LOAD 4", "MUL", "PRINT"]
    operation2 = ["LOAD 4", "LOAD 5", "ADD", "LOAD 2", "LOAD 3", "ADD", 
    "MUL", "PRINT"]
    operation3 = ["LOAD 6", "LOAD 7", "LOAD 4", "SUB", "DIV", "LOAD 5", 
    "MUL", "PRINT"]

    execute(operation1)

if __name__ == '__main__':
    main()

The error I'm getting is:

Traceback (most recent call last):
  File "XXX/EA2-Stack.py", line 53, in <module> main()
  File "XXX/EA2-Stack.py", line 47, in main execute(operation1)
  File "XXX/EA2-Stack.py", line 19, in execute result.push(number)
AttributeError: 'str' object has no attribute 'push'

I don't understand:

1.) Why does it say, that there is no 'push'? I defined a method called 'push'. I also tried to write: push(result, number), but then it says "unresolved reference"

2.) Why does it say Attribute push? It's not an attribute, it's a method.

I've got the feeling, that I'm missing something here :( Thanks for helping me :>

3 Answers 3

2
result = Stack()
result.push(number)

No problem here.

result = result.pop() + result.pop()
result.push(number)

Now there's a problem! result is now a str (as the error message says) instead of a Stack, and indeed str doesn't have the necessary attribute. Use a separate variable name. And yes, result.push is an attribute, even though it's also a method.

Sign up to request clarification or add additional context in comments.

Thanks a lot! I didn't think of this and now it works :)
0

Why does it say, that there is no 'push'?

Because you do not call it on Stack instance. After first command executed, result does not point to Stack instance no more.

Why does it say Attribute push? It's not an attribute, it's a method.

Python interpreter does not care yet. First it checks if attribute with this name exists. After that it tries to call it. In Python functions (therefore methods) are first class citizens, there is nothing special about them.

Comments

0

You are reassigning your result variable to values from the Stack object, thereby destroying your stack object:

result = result.pop() + result.pop()

This destroys your instance of Stack()

Comments

Your Answer

Draft saved
Draft discarded

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.