Computer Science, asked by Gauranshinegi, 4 months ago

class Stack:
def__init__(self):
self.items= []
defis_empty(self):
return self.items== []
defpush(self, data):
self.items.append(data)
defpop(self):
return self.items.pop()
s = Stack()
while True:
print('Press 1 for push')
print('Press 2 for pop')
print('Press 3 for quit')
do = int(input('What would you like to do'))
if do == 1:
n=int(input("enter a number to push"))
s.push(n)
elifdo == 2:
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elifoperation == 3:
break

Answers

Answered by jai696
2

\large\mathsf\color{pink}{Solution\: using\: python\: 3}

class Stack:

def __init__(self):

self.items = []

def push(self, item):

return self.items.append(item)

def pop(self):

return "\nStack is empty!" if self.empty() else f"\nPopped {self.items.pop()}"

def empty(self):

return not self.items

def handle_cmd(do, s):

if do == 2:

return print(s.pop())

return s.push((n := int(input("enter a num: "))))

s = Stack()

while True:

do = int(input("Enter 1 for push, 2 for pop, 3 for quit: "))

if do in [1, 2]:

handle_cmd(do, s)

else:

break

I rewrote the code to better suit my liking. But it supports the same logic as the original.

\large\mathsf\color{lightgreen}useful?\: \color{white}\longrightarrow\: \color{orange}brainliest!

Answered by Japji21
1

Answer:

class Stack:

def __init__(self):

self.items = []

def push(self, item):

return self.items.append(item)

def pop(self):

return "\nStack is empty!" if self.empty() else f"\nPopped {self.items.pop()}"

def empty(self):

return not self.items

def handle_cmd(do, s):

if do == 2:

return print(s.pop())

return s.push((n := int(input("enter a num: "))))

s = Stack()

while True:

do = int(input("Enter 1 for push, 2 for pop, 3 for quit: "))

if do in [1, 2]:

handle_cmd(do, s)

else:

break

I rewrote the code to better suit my liking. But it supports the same logic as the original.

Explanation:

pls follow up

Similar questions