Python goto Package

Source: Internet
Author: User
Tags goto

http://entrian.com/goto/

#Example 1:breaking out from a deeply nested loop: fromGotoImportGoto, Label forIinchRange (1, 10):     forJinchRange (1, 20):         forKinchRange (1, 30):            PrintI, J, KifK = = 3: Goto. Endlabel. EndPrint "finished\n"#Example 2:restarting a loop: fromGotoImportGoto, Labellabel. Start forIinchRange (1, 4):    PrintIifi = = 2:        Try: Output=messageexceptNameerror:Print "oops-forgot to define ' message '! Start again."message="Hello World"Goto. StartPrintOutput"\ n"#Example 3:cleaning up after something fails: fromGotoImportGoto, Label#Imagine that these is real worker functions.defSetUp ():Print "setUp"defDofirsttask ():Print1;returnTruedefDosecondtask ():Print2;returnTruedefDothirdtask ():Print3;returnFalse#This one pretends to fail.defDofourthtask ():Print4;returnTruedefCleanUp ():Print "cleanUp"#This prints "setUp, 1, 2, 3, cleanUp"-no "4" because Dothirdtask fails.defBigFunction1 (): SetUp ()if  notdofirsttask (): Goto. Cleanupif  notdosecondtask (): Goto. Cleanupif  notdothirdtask (): Goto. Cleanupif  notdofourthtask (): Goto. Cleanup label. Cleanup Cleanup () BigFunction1 ()Print "BigFunction1 done\n"#Example 4:using Comefrom to let the cleanup code take control itself. fromGotoImportcomefrom, LabeldefBigFunction2 (): SetUp ()if  notDofirsttask (): label. Failedif  notDosecondtask (): label. Failedif  notDothirdtask (): label. Failedif  notDofourthtask (): label. Failed Comefrom. Failed cleanUp () BigFunction2 ( )Print "BigFunction2 done\n"#Example 5:using A computed goto: fromGotoImportGoto, Labellabel. Getinputi= Raw_input ("Enter either ' a ', ' B ' or ' C ', or any other letter to quit:")ifIinch('a','b','C'): Goto*IElse: Goto. Quitlabel. APrint "You typed ' a '"Goto. Getinputlabel. bPrint "You typed ' B '"Goto. Getinputlabel. CPrint "You typed ' C '"Goto. Getinputlabel. QuitPrint "finished\n"#Example 6:what happens when a label is missing: fromGotoImportGoto, Labellabel. Realgoto. Unreal#raises a Missinglabelerror exception.

Python goto Package

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.