More elegant way to try running a function X times?
Nicholas Ferenc Fabry
nick.fabry.maillists at coredump.us
Wed Nov 19 09:31:48 EST 2008
On Nov 19, 2008, at 09:09, Gilles Ganault wrote:
> Hello
>
> As a newbie, it's pretty likely that there's a smarter way to do this,
> so I'd like to check with the experts:
>
> I need to try calling a function 5 times. If successful, move on; If
> not, print an error message, and exit the program:
>
> =====
> success = None
>
> for i in range(5):
> #Try to fetch public IP
> success = CheckIP()
> if success:
> break
>
> if not success:
> print "Exiting."
> sys.exit()
> =====
>
A little simpler:
for i in range(5):
if CheckIP():
break
else:
print "Exiting."
sys.exit()
The else part will only fire if the for finishes without breaking.
Hope this helps a bit...
Nick Fabry
> Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list