[sage-support] Re: breaking out of double for loops

2009-07-23 Thread Laurent
mac8090 ha scritto: for x in range(10): for y in range(10): if 2^x*3^y==12: break (x,y) I would like that : def foo(): for x in range(10): for y in range(10): if 2^x*3^y==12: return (x,y) The return command exits the

[sage-support] Re: breaking out of double for loops

2009-07-23 Thread Carlo Hamalainen
On Thu, Jul 23, 2009 at 12:31 PM, mac8090bonzerpot...@hotmail.com wrote: How does one break from a double for loop, or a loop of two variables? One way is to use an exception: class GetOut(Exception): pass try: for x in range(10): for y in range(10): if 2^x*3^y==12:

[sage-support] Re: breaking out of double for loops

2009-07-23 Thread Minh Nguyen
On Thu, Jul 23, 2009 at 8:41 PM, Carlo Hamalainencarlo.hamalai...@gmail.com wrote: On Thu, Jul 23, 2009 at 12:31 PM, mac8090bonzerpot...@hotmail.com wrote: How does one break from a double for loop, or a loop of two variables? One way is to use an exception: class GetOut(Exception): pass

[sage-support] Re: breaking out of double for loops

2009-07-23 Thread Marshall Hampton
Two more solutions: #ugly: x,y = 0,0 while 2^x*3^y != 12 and x 10: y = 0 x = x + 1 while 2^x*3^y != 12 and y 10: y = y + 1 #short: for x,y in CartesianProduct(range(10),range(10)): if 2^x*3^y==12: break -Marshall Hampton On Jul 23, 4:31 am, mac8090

[sage-support] Re: breaking out of double for loops

2009-07-23 Thread Craig Citro
for x in range(10):     for y in range(10):          if 2^x*3^y==12:                break (x,y) I think the most pythonic solution would be to use itertools.product, which requires python 2.6 or greater (and hence sage 4.1 or greater): sage: import itertools sage: for x,y in