> 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 iter
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 wrote:
>
On Thu, Jul 23, 2009 at 8:41 PM, Carlo
Hamalainen wrote:
>
> On Thu, Jul 23, 2009 at 12:31 PM, mac8090 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):
>
On Thu, Jul 23, 2009 at 12:31 PM, mac8090 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:
raise Get
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