In what way does it act differently from what you want? We don't know what to look for without that information. "works" does not explain this!
Indentation is part of the code, so make sure that it is included in your post. One should be able to copy your code and run it without modifying it, and get the same result as you.
Make sure to have an idea of what your function should be doing step by step to produce the desired result. Then add prints in your function that explain what it is doing. If you now compare those prints with what you want it to be doing, you'll be able to tell where it goes wrong.