while 循环和小于或等于符号 (Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32417338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
while loop and less than or equal to sign (Python)
提问by Zion
So I was doing while loops and I noticed something strange.
所以我在做 while 循环时发现了一些奇怪的东西。
count = 0
while count <= 5:
count += 1
print(count)
output:
输出:
1
2
3
4
5
6
it's not that I don't understand while loops. It's that how come the count is printed up to six? when it's supposed to print count
only if count
is less than or equal to 5?
并不是我不理解 while 循环。那怎么把计数打印到六呢?count
仅当count
小于或等于 5时才应该打印?
and well 6 is beyond 5. why is this?
井 6 超过 5。这是为什么?
I know I could do
我知道我可以
count = 0
while count != 5:
count += 1
print(count)
but I just want to understand why does putting <=
behave in an odd way?
但我只是想了解为什么 put 的<=
行为方式很奇怪?
采纳答案by Martijn Pieters
There is nothing odd about <=
; your loop condition allows for numbers up to and including5
. But you increment count
and thenprint it, so you will print 6
last.
没有什么奇怪的<=
;您的循环条件允许最多包含5
. 但是你增加count
和再打印出来,这样您将打印6
最后。
That's because count = 5
satisfies your loop condition, then you add one to make it 6
and print. The next time through the loop count <= 5
is no longer true and only thenloop ends.
那是因为count = 5
满足您的循环条件,然后您添加一个使其6
并打印。下一次循环count <= 5
不再为真,然后循环结束。
So your code does this:
所以你的代码是这样的:
count = 0
,count <= 5
->True
,count += 1
makescount = 1
, print1
.count = 1
,count <= 5
->True
,count += 1
makescount = 2
, print2
.count = 2
,count <= 5
->True
,count += 1
makescount = 3
, print3
.count = 3
,count <= 5
->True
,count += 1
makescount = 4
, print4
.count = 4
,count <= 5
->True
,count += 1
makescount = 5
, print5
.count = 5
,count <= 5
->True
,count += 1
makescount = 6
, print6
.count = 6
,count <= 5
->False
, end the loop.
count = 0
,count <= 5
->True
,count += 1
制作count = 1
, 打印1
.count = 1
,count <= 5
->True
,count += 1
制作count = 2
, 打印2
.count = 2
,count <= 5
->True
,count += 1
制作count = 3
, 打印3
.count = 3
,count <= 5
->True
,count += 1
制作count = 4
, 打印4
.count = 4
,count <= 5
->True
,count += 1
制作count = 5
, 打印5
.count = 5
,count <= 5
->True
,count += 1
制作count = 6
, 打印6
.count = 6
,count <= 5
->False
, 结束循环。
You could increment the counter afterprinting:
您可以在打印后增加计数器:
while count <= 5:
print(count)
count += 1
or you could use <
to only allow numbers smallerthan 5
:
或者你可以使用<
,只允许数字小比5
:
while count < 5:
count += 1
print(count)
回答by maazza
It is simple when count equals five you add 1 and it becomes 6 then it is printed and you exit the loop.
很简单,当计数等于 5 时,您加 1 并变为 6,然后将其打印出来并退出循环。
回答by Farbod Salamat-Zadeh
Your problem is not about how <=
works.
你的问题不在于如何<=
运作。
You are adding 1 to count
before printing it, so when count is equal to 5, you then add 1 and therefore print 6.
你count
在打印之前加 1 ,所以当计数等于 5 时,你再加 1,因此打印 6。
回答by Zenohm
Let's walk through the code and see what's happening.
让我们浏览一下代码,看看发生了什么。
Note: If your code is doing something that you didn't expect it to do, this is a good practice to follow.
注意:如果您的代码正在做一些您不希望它做的事情,那么这是一个很好的做法。
count = 0
while count <= 5:
count += 1
print(count)
The count
starts at 0
在count
从0开始
count = 0
while count <= 5: # count = 0. Is 0 <= 5? Yes. Run the code.
count += 1
print(count)
Increment the count so it now equals 1. Print it.
增加计数,使其现在等于 1。打印它。
while count <= 5: # count = 1. Is 1 <= 5? Yes. Run the code.
count += 1
print(count)
Increment. Print. Repeat.
增量。打印。重复。
Let's move on to the interesting case that's causing the problem.
让我们继续讨论导致问题的有趣案例。
while count <= 5: # count = 5. Is 5 <= 5? Yes. Run the code.
count += 1
print(count)
In this case the count
still satisfies the inequality, so the code runs.
在这种情况下count
仍然满足不等式,因此代码运行。
What happens?
发生什么了?
The count
, which is 5, is incremented and thus prints out 6.
的count
,这是5,被递增,因此打印出6。
Now that I hope you understand why the problem exists, let's explore alternatives and their advantages, disadvantages, and outputs.
现在我希望您了解问题存在的原因,让我们探索替代方案及其优点、缺点和输出。
Let's start with your code.
让我们从您的代码开始。
count = 0
while count <= 5:
count += 1
print(count)
Advantages: Does not print out 0
优点:不打印出0
Disadvantages: Prints out 6
缺点:打印出来 6
What if we removed the =
sign?
如果我们去掉这个=
标志怎么办?
count = 0
while count < 5:
count += 1
print(count)
Output:
输出:
1
2
3
4
5
Advantages: Does what you want
优点:做你想做的
Disadvantages: You have to start at 0 instead of 1
缺点:你必须从 0 而不是 1 开始
What if we did as you suggested and replaced the <
sign with the !
sign?
如果我们按照您的建议进行并用<
标志替换!
标志怎么办?
count = 0
while count != 5:
count += 1
print(count)
Output:
输出:
1
2
3
4
5
Advantages: Does what you want
优点:做你想做的
Disadvantages: Fragile. If you ever changed your increment so that it increased by a number like, say, 3, the end point would be skipped over accidentally and the code would continue to run forever. Better to use an inequality
缺点:脆弱。如果您曾经更改过增量以使其增加一个数字,例如 3,终点将被意外跳过并且代码将永远继续运行。最好使用不等式
What if we wanted where we start to be the first number that is displayed? Well, to do this we'd have to print out the current number before we change it, so that means we have to flip the order of the events.
如果我们希望从哪里开始显示第一个数字怎么办?好吧,要做到这一点,我们必须在更改它之前打印出当前数字,这意味着我们必须翻转事件的顺序。
count = 1 # Change the first number so it's what we want displayed first.
while count <= 5:
print(count)
count += 1
Output:
输出:
1
2
3
4
5
Advantages: Does what you want and starts on the first displayed number instead of below it.
优点:做你想做的,从第一个显示的数字开始,而不是在它下面。
Disadvantages: If you want to stick with while
loops, this is the way to go, but there is a better way in this case.
缺点:如果你想坚持使用while
循环,这是要走的路,但在这种情况下有更好的方法。
In situations like this, where you increment numbers and then perform operations with them, it's much more logical to just use a for
loop, which was designed for instances just like this one.
在这种情况下,您增加数字,然后使用它们执行操作,只使用for
循环更合乎逻辑,它是为这样的实例设计的。
for count in range(1,5):
print(count)
Output:
输出:
1
2
3
4
5
Advantages: Does what you want, easier to read, easier to write, less likely to cause bugs based on placement.
优点:做你想做的,更容易阅读,更容易写,不太可能导致基于位置的错误。
Disadvantages: The upper boundary must be known, unlike in a while
loop.
缺点:上边界必须是已知的,不像在while
循环中。
回答by Hasan.Mougharbel
To make things clear i will show the two scenarios with concrete explanation:
为了清楚起见,我将用具体的解释展示这两种情况:
a=0
while (a<4):
a=a+1
print(a)
output would be 1,2,3,4
输出将是 1,2,3,4
Variable a=0 should satisfy the condition(a<4) of while loop to continue the execution.
变量a=0 应该满足while 循环的条件(a<4) 才能继续执行。
a=a+1 is calculated and saved in memory of the loop which is now 1. Therefore the number 1 would be outputted not 0 as a result of print(a). In order to iterate again inside the loop; the number 1(that was saved in the memory of the first loop is checked against the a<4 condition). Which is True; continue execution as previous.
a=a+1 被计算并保存在循环的内存中,现在是 1。因此,作为 print(a) 的结果,数字 1 将被输出而不是 0。为了在循环内再次迭代;数字 1(保存在第一个循环的内存中,根据 a<4 条件进行检查)。这是真的;像以前一样继续执行。
a=a+1 calculated and saved in memory of loop which is now 2. Then the number 2 is outputted and is saved in the memory of the second loop and checked afterwards against the a<4). Which is True; continue execution as previous.
a=a+1 计算并保存在循环的内存中,现在为 2。然后输出数字 2 并保存在第二个循环的内存中,然后根据 a<4) 进行检查。这是真的;像以前一样继续执行。
a=a+1 calculated and saved in memory of loop which is now 3. Then the number 3 is outputted and is saved in the memory of the third loop which is checked afterwards against the a<4). Which is True; continue execution as previous.
a=a+1 计算并保存在循环的内存中,现在是 3。然后输出数字 3 并保存在第三个循环的内存中,然后根据 a<4) 进行检查。这是真的;像以前一样继续执行。
a=a+1 calculated and saved in memory of loop which is now 4. Then the number 4 is outputted, even though the condition a<4 should be satisfied. The reason is that, i have stuck inside the loop after satisfaction of previous condition 3<4 and now after i am inside the loop, the execution of the statements is inevitable which will hold to output 4. The number 4 is now saved in the memory of the fourth loop which is checked against the a<4). Which is False, execution stop here.
a=a+1 计算并保存在循环的内存中,现在是 4。然后输出数字 4,即使应该满足条件 a<4。原因是,我在满足前面的条件 3<4 后卡在循环中,现在在我进入循环后,语句的执行是不可避免的,它将保持输出 4。数字 4 现在保存在第四个循环的内存,根据 a<4) 进行检查。这是 False,执行到此停止。
This scenario would be amenable and understandable to the case of the opposite scenario
这种情况对于相反的情况是可以接受和理解的
a=0
while (a<4):
print(a)
a=a+1
Output would be 0,1,2,3
输出将是 0,1,2,3
Variable a=0 should satisfy the condition(a<4) of while loop to continue the execution.
变量a=0 应该满足while 循环的条件(a<4) 才能继续执行。
First the print statement is executed, and the first value coined by a in the memory of the first loop is 0. Therefore 0 is outputted. Afterwards a=a+1 is calculated and saved in memory of the loop which will change to be 1 and not 0. In order to iterate again inside the loop; the number 1(that was saved in the memory of the first loop is checked against the a<4 condition). Which is True; continue execution as previous.
首先执行print语句,第一个循环的内存中a创造的第一个值是0。因此输出0。之后a=a+1被计算并保存在循环的内存中,它会变成1而不是0。为了在循环内再次迭代;数字 1(保存在第一个循环的内存中,根据 a<4 条件进行检查)。这是真的;像以前一样继续执行。
The print statement is executed, and the value coined by a in the memory of the second loop is now 1. Therefore 1 is outputted. Afterwards a=a+1 is calculated and saved in memory of the loop which is now 2. In order to iterate again inside the loop; the number 2(that was saved in the memory of the second loop is checked against the a<4 condition). Which is True; continue execution as previous.
执行打印语句,第二次循环的内存中由 a 生成的值现在为 1。因此输出 1。之后计算 a=a+1 并保存在循环的内存中,现在为 2。为了在循环内再次迭代;数字 2(保存在第二个循环的内存中,根据 a<4 条件进行检查)。这是真的;像以前一样继续执行。
The print statement is executed, and the value coined by a in the memory of the third loop is now 2. Therefore 2 is outputted. Afterwards a=a+1 is calculated and saved in memory of the loop which is now 3. In order to iterate again inside the loop; the number 3(that was saved in the memory of the third loop is checked against the a<4 condition). Which is True; continue execution as previous.
执行print语句,第三次循环内存中a生成的值现在是2。因此输出2。之后计算 a=a+1 并保存在循环内存中,现在为 3。为了在循环内再次迭代;数字 3(保存在第三个循环的内存中,根据 a<4 条件进行检查)。这是真的;像以前一样继续执行。
First the print statement is executed, and the value coined by a in the memory of the fourth loop is now 3. Therefore 3 is outputted. Afterwards a=a+1 is calculated and saved in memory of the loop which is now 4. In order to iterate again inside the loop; the number 4(that was saved in the memory of the fourth loop is checked against the a<4 condition). Which is False; execution stops.
首先执行print语句,第四次循环内存中a生成的值现在是3。因此输出3。然后计算 a=a+1 并保存在循环的内存中,现在为 4。为了在循环内再次迭代;数字 4(保存在第四个循环的内存中,根据 a<4 条件进行检查)。这是错误的;执行停止。