I'm looking for a good idiom to do the following: given a boolean variable called retry
, if that variable is True
, then run some sequence of instructions inside a while True
loop, and if the variable is False
then just run the sequence of instructions once.
The best I could come up with is:
while True:
# my code
if not retry:
break
The problem I have with this is that while True
is at the top and only at the end and inside the loop you see the logic. It's not very clear in my opinion.
Here's the straightforward one:
if retry:
while True:
# my code
else:
# my code
But then you're duplicating code.
Another bad alternative with code duplication:
# my code
while retry:
# my code
Any ideas?