Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 04:11 PM
Newbie
 
Join Date: Jun 2007
Posts: 15
Default 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
Reply
  #2  
Old August 28th, 2008, 04:36 PM
Expert
 
Join Date: Sep 2007
Posts: 848
Default

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.

Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     print "Hello, World"
  3.     break
  4. #Hello, World is printed once
  5.  
Reply
  #3  
Old September 4th, 2008, 12:58 PM
Member
 
Join Date: Jun 2008
Location: Sweden
Posts: 120
Default

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)
Reply
  #4  
Old September 7th, 2008, 07:04 PM
Subsciber123's Avatar
Member
 
Join Date: Nov 2006
Location: Eastern Time Zone
Posts: 83
Default

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

"if something==something_else:"
works just as well as
"if (something==something_else):"
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles