简体   繁体   English

Python:满足条件时退出while循环

[英]Python : Exit while loop when condition is met

Im trying to iterate over my list and calculate the diff between each element and the element following it.我试图遍历我的列表并计算每个元素与其后面的元素之间的差异。 If the difference is greater than 0 ( or positive ) then increment up and decrease down by 1 ( if it is greater than 0 ).如果差值大于 0(或正数),则up递增并向down 1(如果大于 0)。 Similarly if difference is less than 0 then increment down and decrease up by 1 ( if greater than 0 ).同样,如果差异小于 0,则down递增并向up减少 1(如果大于 0)。 I want to exit out of the loop if either the up or down exceeds limit which is set to 3.如果updown超过设置为 3 的limit ,我想退出循环。

My code:我的代码:

my_list = [13.04, 12.46, 13.1, 13.43, 13.76, 13.23, 12.15, 12.0, 11.55, 14.63]

up = 0
down = 0
limit = 3

while (up < limit) or (down < limit) :
    for i in range(len(my_list)-1):
        diff = my_list[i] - my_list[i+1]
        print (diff)
        if diff > 0:
            up +=1
            if down > 0:
                down -=1
        elif diff < 0:
            down +=1
            if up > 0:
                up -=1

Obviously this is not working since I keep getting caught up in an infinite loop and cant figure out what I am doing wrong.显然这是行不通的,因为我一直陷入无限循环并且无法弄清楚我做错了什么。

The while condition is wrong. while 条件是错误的。 The loop keeps going while either up or down is below limit , so it will not stop even when up=1000 , as long as down<3 .updown低于limit时,循环会继续进行,因此即使up=1000也不会停止,只要down<3 What you want is while (up < limit) and (down < limit) instead.你想要的是while (up < limit) and (down < limit)

do not use while, you can use if condition in last of for loop for break it:不要使用while,你可以在for循环的最后使用if条件来打破它:

for i in range(len(my_list)-1):
    diff = my_list[i] - my_list[i+1]
    #print (diff)
    if diff > 0:
        up +=1
        if down > 0:
            down -=1
    elif diff < 0:
        down +=1
        if up > 0:
            up -=1
    if (up > limit)or (down > limit) :print(up);print(down);break

The problem is with your condition.问题在于你的情况。 You said you want to exit the program if either the up or down exceeds the limit which is set to 3. So the condition on the while loop needs to be set as while up is less than limit AND down is also less than limit , only then execute the body of the loop.你说如果updown超过设置为 3 的limit ,你想退出程序。所以 while 循环的条件需要设置为while up is less than limit AND down is also less than limit ,只有然后执行循环体。 Something like this.像这样的东西。

while up < limit and down < limit:

or you can also use brackets (doesn't matter in this case)或者你也可以使用括号(在这种情况下没关系)

while (up < limit) and (down < limit):

So the full program will be所以完整的程序将是

my_list = [13.04, 12.46, 13.1, 13.43, 13.76, 13.23, 12.15, 12.0, 11.55, 14.63]

up = 0
down = 0
limit = 3

while up < limit and down < limit:
    for i in range(len(my_list)-1):
        diff = my_list[i] - my_list[i+1]
        print (diff)
        if diff > 0:
            up +=1
            if down > 0:
                down -=1
        elif diff < 0:
            down +=1
            if up > 0:
                up -=1

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

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