How does Python jump out of multiple loops?

Source: Internet
Author: User

I encountered this problem when I wrote the Euler's question tonight. I need to jump out of a triple loop, but it is really helpless. There is no good way. The following isCode:

 Import  Time  Def  Isprime (n ):  """  This function return a number is a prime or not  """      From Math Import  SQRT  For I In Range (2, INT (SQRT (N) + 1 ): If N % I = 0:  Return  False  Return  True  Def  Permu (m, n ):  """  This function return if two numbers has the same digits.  """  MS = List (STR (M) ms. Sort () NS = List (STR (N) ns. Sort () If MS = NS:  Return  True  Else  :  Return  False Start = Time. Clock () primelist = [I For I In Range (1000,999 9) If  Isprime (I)] primeset = Set (primelist) N =Len (primelist) count = 0  While Count! = 2 :  For I In Range (0, N-2 ):  For J In Range (I + 1, n-1 ): B = (Primelist [I] + primelist [J])/2 If B In  Primeset: If Permu (primelist [I], primelist [J]) And  Permu (primelist [I], INT (B): Count + = 1 If Count = 2 :  Print (STR (primelist [I]) + STR (INT (B) + STR (primelist [J])  Print (Time. Clock ()-start)

 

The intention is to jump out of this triple loop when Count = 2, and the results show that no matter how you write it. Therefore, you can only jump out with a limit on while.

 

The following are other people's ideas on how to jump out of multiple cycles.

1. You can insert an exception to jump out.

Class Foundexception (exception ): Pass  Try  :  For Row, record In  Enumerate (table ):  For Columu, Field In  Enumerate (record ):  For Index, item In  Enumerate (field ):  If Item =Target:  Raise  Foundexception ()  Except  Foundexception:  Print ( "  Found at ({0}, {1}, {2 })  "  . Format (row, column, index ))  Else  :  Print ( '  Not found ' )

 

Second, put it in the function body and return it using the return statement.

 Def  Test ():  For X In Range (9, 0,-1 ):  For Y In Range (9,-1,-1 ):  For Z In Range (9,-1,-1 ): S = 100001 * x + 10010 * Y + 1100 *Z S1 = Int (round (S * 0.5), 0) + 1 For I In Range (S1, 100,-1 ):  If S % S1 = 0:  Print  S, I  Return S

 

Of course, if it is a while loop, you can add a flag to the top layer as I do, and then jump out.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.