All right, here's the result. I simply used
until z>=y
this instead, and as you'd expect, the 19th cycle (i.e., the 91st shot) ended in just one shot, because z was way larger than y. Good news is that with the 20th cycle, the script continued to go as expected, with both z and y reset to the much higher number.
This is workable for me, because I know that all I need to do is to remove every 91st shot before I batch process the images.
In the mean time, I would greatly appreciate it if you can look into the issue. I will also try your script and see what happens, and get back to you.