简体   繁体   English

使用while退出for循环

[英]Using while to exit for loop

I have this code that prints first ten elements of the list and then prints end:我有这段代码打印列表的前十个元素,然后打印结束:

a = ["11","12","13","14","15","16","17","18","19","110"]
n = 0
limit= 10
while n != limit:
    for b in a:
        if "1" in b:
            print(b)
            n += 1
print("end")

I am trying to figure out why it breaks if I add more numbers to the list.我试图弄清楚如果我在列表中添加更多数字,它为什么会中断。

a = ["11","12","13","14","15","16","17","18","19","110","111","112"]
n = 0
limit= 10
while n != limit:
    for b in a:
        if "1" in b:
            print(b)
            n += 1
print("end")

Goes on forever, printing the numbers again and again.永远持续下去,一次又一次地打印数字。 Can someone explain why?有人可以解释为什么吗?

I have already replaced it with this but I just want to understand what was wrong with the first one.我已经用它替换了它,但我只想了解第一个有什么问题。

a = ["11","12","13","14","15","16","17","18","19","110","111","112"]
n = 0
limit= 10
for b in a:
    if "1" in b:
        print(b)
        n += 1
    if n == limit:
        break
print("end")

What was wrong with it?它有什么问题?

You add more than 1 at each turn in the ( while ) loop , so before the loop, n is smaller thant 10 , and after, it is greater than n .在 ( while ) 循环中的每一轮都添加超过 1 ,因此在循环之前, n小于10 ,之后,它大于n Thus, never equals 10 .因此,从不等于10

Just change the condition to >= (or > depending on what you want).只需将条件更改为>= (或>取决于您想要的)。

I am not still convinced that whatever you are doing is right.我仍然不相信你所做的任何事情都是正确的。 If fr sure you want to print the first 10 elements in the list, use this approach:如果您确定要打印列表中的前 10 个元素,请使用以下方法:

a = ["1", "12", "13", "14", "15", "16", "17", "18", "19", "110", "111", "112"]

limit = 10
for i in a[0:limit]:
    print(i)
print("end")

I have this code that prints first ten elements of the list and then prints end我有这段代码打印列表的前十个元素,然后打印结束

That's not what your original code does.这不是您的原始代码所做的。 The for loop will iterate over all elements of the list, no matter how long. for循环将遍历列表的所有元素,无论多长时间。 So, the while condition is only checked after you've iterated over the entire list.因此,只有在您遍历整个列表后才会检查while条件。

So, when the code starts, the first while check is before any looping, and checks 0 != 10 .因此,当代码开始时,第一个while检查在任何循环之前,并检查0 != 10 It's then not checked again until the for loop has visited every element of the list.然后在for循环访问列表的每个元素之前不会再次检查它。 Because your first list has exactly 10 elements with "1" in them, it turns out n will be exactly 10, and the second time the while condition is tested, it will be 10 == 10 .因为您的第一个列表正好有 10 个元素,其中包含“1”,所以结果n正好是 10,而第二次测试while条件时,它将是10 == 10 But in your second example, where you have 11 elements, the while condition won't be checked until n is 11.但是在您的第二个示例中,您有 11 个元素,直到n为 11 才会检查while条件。

Try putting print statements in various spots to see what's going on, eg just after the while statement, just after the for statement, just after the if statement.尝试将 print 语句放在不同的位置以查看发生了什么,例如,就在while语句之后,就在for语句之后,就在if语句之后。 That will show you which statements are executed when.这将向您显示何时执行哪些语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM