function values are not printing
When I execute this code it doesn't print 'current stack numbers []
' values.
But when I Uncomment the #s = c()
it works, what is the reason?
def c():
stack = []
return stack
def check_empty(stack):
return len(stack) == 0
def push(stack, item):
stack.append(item)
print("pushed item" + item)
def pop(stack):
if (check_empty(stack)):
return "stack is emtpy"
return stack.pop()
#s = c()
push(c(), str(1))
push(c(), str(2))
push(c(), str(3))
print("popped item" , pop(c()))
print("current stack numbers" + str(c()))
Given that your function c()
returns an empty list, the last lines of your code becomes
push([], str(1))
push([], str(2))
push([], str(3))
print("popped item" , pop([]))
print("current stack numbers" + str([]))
Which will print nothing, as expected.
However, if you set the output from c()
to a variable and push/pop
to it, you will get the expected behaviour
s = c()
push(s, str(1)) # c = ["1"]
push(s, str(2)) # c = ["1", "2"]
push(s, str(3)) # c = ["1", "2", "3"]
print("popped item" , pop(s)) # prints "popped item3"
print("current stack numbers" + str(s)) # prints "current stack numbers['1', '2']