在 Python 中退出 while 循环 [英] Exit while loop in Python

查看:152
本文介绍了在 Python 中退出 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我希望 while 循环在 a + b + c<后立即退出/code> = 1000.但是,使用 print 语句进行的测试表明它会一直持续到 for 循环完成.我试过 while True 然后在 if 语句中设置 False 但这会导致无限循环.我认为使用 x = 0 然后设置 x = 1 可能会起作用,但这也只会运行直到 for 循环完成.最优雅、最快捷的退出方式是什么?谢谢.

a = 3乙 = 4c = 5x = 0而 x != 1:对于范围内(3,500):对于范围内的 b(a+1,500):c = (a**2 + b**2)**0.5如果 a + b + c == 1000:打印 a, b, c打印 a*b*cx = 1

解决方案

while 循环只会在控件返回时匹配条件,即当 for> 循环完全执行.所以,这就是为什么您的程序即使满足条件也不会立即退出的原因.

但是,如果 a,b,c 的任何值都不满足条件,那么您的代码将以无限循环.

您应该在此处使用一个函数,因为 return 语句将执行您的要求.

def func(a,b,c):对于范围内(3,500):对于范围内的 b(a+1,500):c = (a**2 + b**2)**0.5如果 a + b + c == 1000:打印 a, b, c打印 a*b*creturn # 使您的函数退出,并向调用者返回一个值功能(3,4,5)

除了@Sukrit Kalra 的 answer 之外,他使用了退出标志,您还可以使用 sys.exit() 如果您的程序在该代码块之后没有任何代码.

导入系统一 = 3乙 = 4c = 5对于范围内(3,500):对于范围内的 b(a+1,500):c = (a**2 + b**2)**0.5如果 a + b + c == 1000:打印 a, b, c打印 a*b*csys.exit() #停止脚本

sys.exit 的帮助:

<预><代码>>>>打印 sys.exit.__doc__退出([状态])通过提高 SystemExit(status) 退出解释器.如果省略状态或无,则默认为零(即成功).如果状态为数字,则将用作系统退出状态.如果是另一种物体,它会被打印出来,系统退出状态将为 1(即失败).

In the code below, I'd like the while loop to exit as soon as a + b + c = 1000. However, testing with print statements shows that it just continues until the for loops are done. I've tried while True and then in the if statement set False but that results in an infinite loop. I thought using x = 0 and then setting x = 1 might work but that too just runs until the for loops finish. What is the most graceful and fastest way to exit? Thanks.

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

解决方案

The while loop will match the condition only when the control returns back to it, i.e when the for loops are executed completely. So, that's why your program doesn't exits immediately even though the condition was met.

But, in case the condition was not met for any values of a,b,c then your code will end up in an infinite loop.

You should use a function here as the return statement will do what you're asking for.

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)

Apart from @Sukrit Kalra's answer, where he used exit flags you can also use sys.exit() if your program doesn't have any code after that code block.

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

help on sys.exit:

>>> 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).

这篇关于在 Python 中退出 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆