User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 422,416 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,118 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 263 | Replies: 2
Reply
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

"break"ing out of multiple levels?

  #1  
May 18th, 2008
For instance, if you have the following (random example):

  1. w = input("Enter width")
  2. h = input("Enter height")
  3.  
  4. for x in range(0,w):
  5. for y in range(0,h):
  6. print x,y
  7. if (raw_input("Stop? y/n") == "y"):
  8. break

That would only break out of the y loop. You could introduce another variable, and change the loop to more or less this form:

  1. for x in range(0,w):
  2. for y in range(0,h):
  3. print x,y
  4. stop = raw_input("Stop? y/n")
  5. if stop == "y":
  6. break
  7. if stop == "y":
  8. break
But that's pretty clumsy, and there are a few complicated cases where it could get very complicated, and you'd need to introduce a flag. Instead, is there any way to tell the break to break out of two levels instead of just one?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 113
Reputation: Freaky_Chris is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is online now Online
Junior Poster

Re: "break"ing out of multiple levels?

  #2  
May 18th, 2008
One way is to raise you own exception and then catch it with a break.

  1. class myException(Exception): pass
  2. w = input("Enter width")
  3. h = input("Enter height")
  4.  
  5. for x in range(0,w):
  6. for y in range(0,h):
  7. print x,y
  8. if (raw_input("Stop? y/n") == "y"):
  9. raise myException
  10. except myException:
  11. break

any help?

Chris
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,460
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: "break"ing out of multiple levels?

  #3  
May 18th, 2008
Simply put the nested loop into a function and use return to break out ...
  1. def exit_nested_loop():
  2. w = h = 100 # for testing
  3. for x in range(0, w):
  4. for y in range(0, h):
  5. print x, y
  6. stop = raw_input("Stop? y/n")
  7. if stop == "y":
  8. return
  9.  
  10. exit_nested_loop()
If you want to use try/except you can do it this way ...
  1. w = h = 100 # for testing
  2. try:
  3. for x in range(0, w):
  4. for y in range(0, h):
  5. print x, y
  6. if raw_input("Stop? y/n") == "y":
  7. raise StopIteration()
  8. except StopIteration:
  9. pass
C used to have the much frowned upon goto statement for such a thing.
Last edited by vegaseat : May 18th, 2008 at 7:43 pm. Reason: C
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 9:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC