gh-38209: Fix infinite loop bug in doctest from #36581
Discovered by tornaria [here](https://github.com/sagemath/sage/pull/3658
1/files/5c51f54b1a193c3f9964da3e1388efd87623d45c#r1575566025).
Sorry for the incorrect implementation! Please merge this quickly,
before the next beta (I think).
URL: https://github.com/sagemath/sage/pull/38209
Reported by: grhkm21
Reviewer(s): Travis Scrimshaw