The main problem is that you are relying on Python's pass by reference for lists to modify the list in place. That works fine initially, but when you reach
plunder(aList[:-1], c-r)
Python creates a copy of the list and proceeds to modify that copy. Thus your original list remains unchanged after Diamonds are exhausted (you hit the else
part).
Note that you can see this behaviour in your printed aList
as only last entry is a list
and all other are tuples.
[
('Silver', 5, 200),
('Gold', 10, 500),
('Platinum', 20, 1000),
['Diamond', 0, 2000] #Only list
]
If you add a print alist[-1]
statement to the function, you can see it even more clearly.
['Diamond', 2, 2000]
['Diamond', 1, 2000]
['Diamond', 0, 2000]
['Platinum', 20, 1000]
['Platinum', 19, 1000]
['Platinum', 18, 1000]
['Platinum', 17, 1000]
['Platinum', 16, 1000]
['Platinum', 15, 1000]
['Platinum', 14, 1000]
['Platinum', 13, 1000]
['Platinum', 12, 1000]
So your algorithm does work but as you have no way of keeping the result, it does not (fully) affect your original list.