It does no such thing!
But it does exactly as you say.
Other humans often correct our instructions and do "the right thing", often without noticing themselves that they didn't do as asked.
So read your code, carry out the instructions. Make no assumptions, do exactly as told.
Did you end up with the right result? If so, then your logic adds up.
If at this point the program doesn't come up with the same result, then you've misunderstood what something does. Add print statements in your code to see for yourself what effect the instructions had, this will let you narrow down the possible things that could have gone wrong.
Then compare what it did and what you want it to do. Perhaps now it is obvious what you need to change. If not, look up what you are trying to do. There is a company called Google, they make a living off of these kinds of situations, they make quite a bit of money so there's probably something to it. Make use of that.
Also, look at your post. The formatting of your code is messed up, some may decline to read it since may not contain the relevant information (it's different from what you are trying to run) and it shows lack of effort on your side.