When a problem asks you to write a function, then just include the definition of that function in the Moodle answer box, not the code the you used to test the function.
If you have included code, perhaps in a main function, that calls the function to test it, then my test code calls your function and your code calls the function a second time making the while loop in the function execute again since the function has been called twice.