簡體   English   中英

了解 python while 循環中具有不同結果的 2 個相似代碼之間的差異

[英]Undertanding the disimilarities between 2 similar codes with different results in python while loop

我是編碼新手,正在嘗試在 while 循環上學習 python。 所以教程中有這段代碼,當我嘗試在我的鍵盤(spyder)上執行它時,它會產生不同的結果。 我的代碼可能做錯了什么

本教程的代碼

在第 13 行增加x ,然后在第 14 行打印x+1

因此,您在循環的每次迭代中得到兩個打印,以及 X=6 的 output (據我了解,這里有什么困擾您)。

您可以更改代碼以在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM