Good python equivalent to C goto

M

Matthew Fitzgibbons

Paul said:
Close, but there is no reason for the conditions to raise anything,
they can just use the continue statement:

i = 20
while i > 0:
try:
if i % 2:
continue
if i % 3:
continue
print i, "is even and a multiple of 3"
finally:
i -= 1

Prints:
18 is even and a multiple of 3
12 is even and a multiple of 3
6 is even and a multiple of 3

I think this is closest to the OP's stated requirements.

-- Paul

I'm aware my example where I raised exceptions was functionally
different ("This will bail out of the loop on an exception and the
exception will get to the next level.") Just thinking in terms of
telling the caller something went wrong. The other examples were
functionally equivalent, however.

Still, this is the best way so far. :) I never thought of using continue
with finally. It gets added to my Python tricks file for future use.

-Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top