This code is not running,mam? i am tryed in lot of many time but output not get in i am so very tried

class Stack:
def init(self,limit):
self.limit = limit
self.elements = []

def push(self):
    if self.isfull():
        print("Stack is full")

    else:
        self.elements.append(element)

def pop(self):
    if self.isempty():
        print("Stack is empty")
    else:
        poped = self.elements.pop()
        print(poped, "poped from the stack")

def isfull(self):
    if len(self.elements) == self.limit:
        print("True")
    else:
        print("False")

def isempty(self):
    if len(self.elements) == 0:
        print("True")
    else:
        print("Flase")

def peek(self):
    return self.elements[-1]  

def delete(self):
    for i in range(len(self.elements)):
        self.elements.pop()      

def print_stack(self):
    return self.elements              

stacks = Stack(6)
stacks.push(1)
stacks.print_stack()

hello @praveenkumar01092001 ,

  1. Push function
    you are saying self.isfull(), instead the syntax is if isfull(self) or if isfull()
    but only this won’t work as for if statement to work it needs boolean response, but function isfull() will always return None as there is no return statement in isfull(). You are printing True/False instead of returning True/False.
    Same goes with pop and isempty.
    Also you need one more argument in push function for the value you are appending(element)
1 Like

@praveenkumar01092001
Hello!
I have found one error that push method should take an argument element which is the value to be pushed onto the stack. Currently, it does not have this argument.

Please try out below corrected code.

class Stack:
    def __init__(self, limit):
        self.limit = limit
        self.elements = []

    def push(self, element):
        if self.isfull():
            print("Stack is full")
        else:
            self.elements.append(element)

    def pop(self):
        if self.isempty():
            print("Stack is empty")
        else:
            popped = self.elements.pop()
            print(popped, "popped from the stack")

    def isfull(self):
        return len(self.elements) == self.limit

    def isempty(self):
        return len(self.elements) == 0

    def peek(self):
        return self.elements[-1]

    def delete(self):
        for i in range(len(self.elements)):
            self.elements.pop()

    def print_stack(self):
        return self.elements

stack = Stack(6)
stack.push(1)
print(stack.print_stack())  # Output: [1]

1 Like