•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 0
For instance, if you have the following (random example):
That would only break out of the y loop. You could introduce another variable, and change the loop to more or less this form:
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?
Python Syntax (Toggle Plain Text)
w = input("Enter width") h = input("Enter height") for x in range(0,w): for y in range(0,h): print x,y if (raw_input("Stop? y/n") == "y"): 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:
Python Syntax (Toggle Plain Text)
for x in range(0,w): for y in range(0,h): print x,y stop = raw_input("Stop? y/n") if stop == "y": break if stop == "y": break
One way is to raise you own exception and then catch it with a break.
any help?
Chris
Python Syntax (Toggle Plain Text)
class myException(Exception): pass w = input("Enter width") h = input("Enter height") for x in range(0,w): for y in range(0,h): print x,y if (raw_input("Stop? y/n") == "y"): raise myException except myException: break
any help?
Chris
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,460
Reputation:
Rep Power: 10
Solved Threads: 176
Simply put the nested loop into a function and use return to break out ...
If you want to use try/except you can do it this way ...
C used to have the much frowned upon goto statement for such a thing.
python Syntax (Toggle Plain Text)
def exit_nested_loop(): w = h = 100 # for testing for x in range(0, w): for y in range(0, h): print x, y stop = raw_input("Stop? y/n") if stop == "y": return exit_nested_loop()
python Syntax (Toggle Plain Text)
w = h = 100 # for testing try: for x in range(0, w): for y in range(0, h): print x, y if raw_input("Stop? y/n") == "y": raise StopIteration() except StopIteration: pass
Last edited by vegaseat : May 18th, 2008 at 7:43 pm. Reason: C
May 'the Google' be with you!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: cant find libpython2.4.so on MacOSX
- Next Thread: True and False Problem



Linear Mode