2013-05-20 248 views
6

在下面的代码,我想的while环尽快a + b + c = 1000退出。但是,使用print语句进行测试表明,它只会持续到for循环完成。我已尝试while True,然后在if语句集False中,但这会导致无限循环。我认为使用x = 0,然后设置x = 1可能会工作,但也只是运行,直到for循环完成。什么是最优雅和最快的退出方式?谢谢。退出while循环在Python

a = 3 
b = 4 
c = 5 
x = 0 
while x != 1: 
    for a in range(3,500): 
     for b in range(a+1,500): 
      c = (a**2 + b**2)**0.5 
      if a + b + c == 1000: 
       print a, b, c 
       print a*b*c 
       x = 1 
+3

忽略了循环问题......你将拥有浮点问题在这里...具体而言,C是浮点#等你将要么:围绕它,投它,或检查abs(a + b + c-1000)<0.00001(或其他一些epsilon)。否则我可能完全错误。 – Foon

+0

'c'是浮动的,但它似乎并不重要。程序发现'a + b + c = 1000'显然不关心'1000'和'1000.0'。很高兴知道,因为它有时可能会成为问题。 – caadrider

回答

6

while循环只会匹配时返回到它,即当for循环完全执行的情况。所以,这就是为什么你的程序即使满足条件也不会立即退出。

但是,如果条件不符合a,b,c的任何值,那么您的代码将以无限循环结束。

您应该使用这里的函数,因为return语句将完成您要求的操作。

def func(a,b,c): 
    for a in range(3,500): 
     for b in range(a+1,500): 
      c = (a**2 + b**2)**0.5 
      if a + b + c == 1000: 
       print a, b, c 
       print a*b*c 
       return # causes your function to exit, and return a value to caller 

func(3,4,5) 

除了@Sukrit卡尔拉的answer,在那里他用出口标志,你也可以使用sys.exit()如果你的程序没有该代码块之后的任何代码。在sys.exit

import sys 
a = 3 
b = 4 
c = 5 
for a in range(3,500): 
    for b in range(a+1,500): 
     c = (a**2 + b**2)**0.5 
     if a + b + c == 1000: 
      print a, b, c 
      print a*b*c 
      sys.exit()  #stops the script 

帮助:

>>> print sys.exit.__doc__ 
exit([status]) 

Exit the interpreter by raising SystemExit(status). 
If the status is omitted or None, it defaults to zero (i.e., success). 
If the status is numeric, it will be used as the system exit status. 
If it is another kind of object, it will be printed and the system 
exit status will be one (i.e., failure). 
+0

谢谢。如果我使用函数,我根本不需要'while'循环。据我所知,该函数将运行,直到返回条件满足,然后它将退出。这工作正常,但并没有真正帮助我理解为什么我原来的'while'循环不会退出。 – caadrider

+0

它退出。它只是做了一些额外的for循环。 –

+0

当for循环完成时,您的代码将停止执​​行。然而,在OP的帖子中,如果在任何情况下都不符合if条件,则循环将从头开始。 – pascalhein

1

可以重构内码成一个函数,并使用return退出:

def inner(): 
    for a in range(3,500): 
     for b in range(a+1,500): 
      c = (a**2 + b**2)**0.5 
      if a + b + c == 1000: 
       print a, b, c 
       print a*b*c 
       return False 
    return True 

while inner(): 
    pass 

看一看this问题。

1

问题是,即使你设置x = 1当a + b + c == 1000时,当条件满足时,你不会摆脱两个for循环,所以while循环不知道即x == 1,直到两个for循环结束。为了避免这个问题,你可以在for循环中添加显式的break语句(正如Sukrit Kalra指出的那样,while循环变得没有必要)。

a = 3 
b = 4 
c = 5 
x = 0 
for a in range(3,500): 
    for b in range(a+1,500): 
    c = (a**2 + b**2)**0.5 
    if a + b + c == 1000: 
     print a, b, c 
     print a*b*c 
     x = 1 
     break 
    if x==1: 
    break 
+0

在这种情况下,你实际上不需要'while'循环。 –

+0

对。感谢您指出了这一点! –

0

您可以try/excepraise包裹时满足条件。

class FinitoException(Exception): 
    pass 

a = 3 
b = 4 
c = 5 
x = 0 
try: 
    for a in range(3,500): 
     for b in range(a+1,500): 
      c = (a**2 + b**2)**0.5 
      if a + b + c == 1000: 
       print a, b, c 
       print a*b*c 
       raise FinitoException() 
except FinitoException: 
    return # or whatever 
+0

我得等到我的Python为这个更好的时候。谢谢你的提示。我会研究'try'语句。还没有学到这些。 – caadrider

3

如果你不想做一个函数(你应该参考Ashwini的答案),下面是一个替代实现。

>>> x = True 
>>> for a in range(3,500): 
     for b in range(a+1, 500): 
      c = (a**2 + b**2)**0.5 
      if a + b + c == 1000: 
       print a, b, c 
       print a*b*c 
       x = False 
       break 
     if x == False: 
      break 
200 375 425.0 
31875000.0 
+0

这是有效的。我希望有一种方法可以一举突破嵌套循环。我认为'while'循环会提供这个选项,但到目前为止我无法弄清楚如何使它工作。 – caadrider

+0

您将无法迅速摆脱所有嵌套循环。但是,如果你在脚本中,你可以使用'sys.exit()',它将在文件的完整执行中脱离出来,并在其后面执行任何操作。 –

+1

@caadrider Python没有像'C'这样的'goto'语句,所以你不能突然跳出嵌套循环。所以这里一个好的选择是使用函数。 –

0

你可以使用一个break语句:

a = 3 
b = 4 
c = 5 
x = 0 
while x != 1: 
    for a in range(3,500): 
     for b in range(a+1,500): 
      c = (a**2 + b**2)**0.5 
      if a + b + c == 1000: 
       print a, b, c 
       print a*b*c 
       break