I thought I understood global and local variables, but apparently not. Similar questions seem to hinge around altering a global variable, however mine is being altered and it shouldn't be.
I have a list with some numbers in and I want to use these numbers to create a new list, without altering the initial list.
deductions = 3
gross = [5,5,5,5]
net = net_result(gross, deductions)
def net_result(G, D):
Nets = G
Deduct = D
for each in range(0,4):
if Deduct > Nets[each]:
Deduct = Deduct - Nets[each]
Nets[each] = 0
else:
Nets[each] = Nets[each] - Deduct
Deduct = 0
return (Nets, Deduct)
In the code above, the output for net is correct but then gross is also being reduced at the same time. Resulting in gross and net being equal to [2,5,5,5], when it should be gross[5,5,5,5] and net[[2,5,5,5],0]
This just seems wrong to me but I can't work out what I'm not getting. Otherwise I have to reset the Gross variables or I can't call them again