简体   繁体   English

了解 python while 循环中具有不同结果的 2 个相似代码之间的差异

[英]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我的代码可能做错了什么

the tutorial"s code本教程的代码

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.

相关问题 我在 python 中的 while 循环和 for 循环得到了不同的结果(检查代码) - I got different results for a while loop and for loop in python (check the codes) Python while 循环 - 相似代码中的差异 - Python while loop - differences in similar codes python和mysql对于类似命令的结果是不同的 - Results are different between python and mysql for similar command 这两个python代码有什么区别? 为什么结果不同? - what's the difference between these 2 python codes? why different results? 为什么重复()在相同的 dataframe 的相似代码上显示不同的结果? - Why is duplicated() showing different results on similar codes, of the same dataframe? 具有相同结果的相同代码 - Identical codes with different results While循环给出不同的返回和打印结果? - While loop giving different return and print results? 四舍五入两个相似的数字并得到不同的结果Python - Rounding two similar numbers and getting different results Python 在Python while循环中使用= rand(between)拉取Excel工作表,并将结果导出为.dbf - pulling an excel sheet with =rand(between) in a Python while loop, and exporting results as .dbf 发送不同的json请求但在Python中获得相似的结果 - Sending different json requests but getting similar results in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM