[英]Undertanding the disimilarities between 2 similar codes with different results in python while loop
Am new to coding, am trying to learn python-currently on the while loop.我是编码新手,正在尝试在 while 循环上学习 python。 So there is this code on the tutorial that when i try to excute it on my codepad(spyder) it generates a different result.所以教程中有这段代码,当我尝试在我的键盘(spyder)上执行它时,它会产生不同的结果。 What might i be doing wrong mycode我的代码可能做错了什么
You increment x
in line 13, and then you print x+1
in line 14.在第 13 行增加x
,然后在第 14 行打印x+1
。
Therefore you get two print in each iteration of the loop, and the output of X=6 (what is what's bothering you here, from what I understand).因此,您在循环的每次迭代中得到两个打印,以及 X=6 的 output (据我了解,这里有什么困扰您)。
You can change your code to have an additional print after the while loop ends:您可以更改代码以在 while 循环结束后进行额外打印:
x = 0
while x < 5:
print("Not yet there, X=" + str(x))
x += 1
print("X=" + str(x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.