简体   繁体   中英

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. So there is this code on the tutorial that when i try to excute it on my codepad(spyder) it generates a different result. 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.

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).

You can change your code to have an additional print after the while loop ends:

x = 0
while x < 5:
    print("Not yet there, X=" + str(x))
    x += 1
print("X=" + str(x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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