I got a short script that is supposed to simulate a stack. The script consists of two functions, one for the push and one for the pop operation. The functions are called once per operation.
My issue is that apparently for the pop-function the global variable for the stack does not get updated by the output of the pop-function and I just don't get why that is, as the push-function works fine and to me it looks like the pop-function works in an analogous fashion.
I have looked into other threads that talk about how to instantiate global variables with the return values of the functions that got called with the viable as the parameter, but apparently I am missing something.
Could some body point me to the issue with my script and what my misconception about passing values between local and global variables is?
So this is my script:
l = []
def push(l_in,a):
l_in.append(a)
l_out = l_in
print l_out
return l_out
def pop(l_in):
length = len(l_in)
if length == 0:
print []
else:
l_in = l_in[0:length-1]
print l_in
return l_in
But for this request:
push(l,'a')
push(l,'b')
push(l,'c')
pop(l)
pop(l)
pop(l)
I get this output:
['a']
['a', 'b']
['a', 'b', 'c']
['a', 'b']
['a', 'b']
['a', 'b']
So push is working, pop not. But I don't undertsand what the difference is in terms of passing values from the function to the global variable.
Thanks