Nested loops and goto line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eddiefisher41
    New Member
    • Jun 2007
    • 15

    Nested loops and goto line

    Hello.
    I have a quick question about loops, i just cant seem to get it to work correctly. the code generates a tuple containing a set of numbers. The if statement is designed to catch tuples that are not valid, if its not a valid one then i need it to go back to line 1 and regenerate the tuple.

    def function():
    ............... ............... # line1
    ............... ............... # line2
    ............... ............... # line3
    ............... ............... # line4

    if (logical statement = true):
    #goto line 4

    Now rather than putting those 4 lines into another function and messing up my counter scheme id rather use some kind of loop structure.
    I have tried using a while loop:

    while (logical statement = true):
    ............... ............... # line1
    ............... ............... # line2
    ............... ............... # line3
    ............... ............... # line4

    But this comes problematic when it needs to exit the loop and gives infinate loops, YUK!

    Any help would be much appreciated.
    Ed
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    If this is in a function, and the function is supposed to return the valid tuple, then when you know it's valid, just return it. Python does support a break statement, as well, that will exit the innermost loop surrounding it.

    [CODE=python]
    while 1:
    print "Hello, World"
    break
    #Hello, World is printed once
    [/CODE]

    Comment

    • asedt
      New Member
      • Jun 2008
      • 130

      #3
      I can't se any wrong in the way you think, if you use the same logical statment as before then it shold work.

      If the tuple is right the logical statments gets false and the while loop ends.

      (You can ommit the "= true", // logical values don't change when you "XNOR" them with true)

      Comment

      • Subsciber123
        New Member
        • Nov 2006
        • 87

        #4
        Also, you don't need all the parentheses around everything.

        "if something==some thing_else:"
        works just as well as
        "if (something==som ething_else):"

        Comment

        Working...