Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate stack value's
#5
would this then mean that i take poped element then try to add to with a symbol who is for example + and add it with the other poped element and get result ? and where exactly should i write this command ?
meanwhile i was trying to upgrade my own:


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

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

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

    def get_stack(self):
        return self.items

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

    def is_full(self):
        return self.items == [1]

    def size(self):
        return len(self.items)



    def eval(self):
        s = stack()
        for symbol in self:
            if symbol in "0123456789":
                s.append(int(symbol))

            plus = None
            elif not s.is_empty():
            if symbol == "+":
                plus = s.push() + s.push()
            elif symbol == "-":
                plus = s.push() - s.push()
            elif symbol == "*":
                plus = s.push() * s.push()
            elif symbol == "/":
                plus = s.push() / s.push()

        if plus is not None:
            s.append(plus)
        else:
            raise Exception("unknown value %s" % symbol)

    return s.pop()


s = stack()
print("Is stack empty?")
print(s.is_empty())
print("Is stack Full?")
print(s.is_full())
s.push(9)
s.push('+')
s.push(5)
s.push('+')
s.push(8)
s.push('+')
s.push(7)

s.push('+')
s.push(3)
print(s.size())
print(s.get_stack())
print(s.eval())

s.pop()
s.pop()
print(s.get_stack())
but ran into a problem where it does not recognize this comand and i can seem to figure out why:
Error:
elif not s.is_empty(): ^ SyntaxError: invalid syntax
Reply


Messages In This Thread
Calculate stack value's - by GFreenD - May-12-2019, 06:15 PM
RE: Calculate stack value's - by michalmonday - May-12-2019, 06:32 PM
RE: Calculate stack value's - by GFreenD - May-12-2019, 06:55 PM
RE: Calculate stack value's - by michalmonday - May-12-2019, 07:15 PM
RE: Calculate stack value's - by GFreenD - May-12-2019, 07:53 PM
RE: Calculate stack value's - by Yoriz - May-12-2019, 08:07 PM
RE: Calculate stack value's - by GFreenD - May-12-2019, 08:40 PM
RE: Calculate stack value's - by Yoriz - May-12-2019, 09:02 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020