I have a question! Python.
why doesn't the loop stop at 5? the condition is that it should stop when the variable i is <= 5 https://code.sololearn.com/cIJyWqy5uNdt/?ref=app
11/30/2020 8:56:56 AM
๐_M_๐
28 Answers
New AnswerBecause it adds one after it does the test. When i gets to 6 it fails the test and the loop stops and i is printed.
i = 1 while i <=5: print(i) i = i + 1 Here, the system stops after i is 5 because any number higher than 5 will not be true in <=5. For example: if i was 6, the command would have no output, since 6 is higher than 5 Edit: ๐_M_๐ Check my comment again.I answered to the second code๐๐ป
Hi, that's because you're using this (<=) operator, while loop adds 1 to i every time it validates its condition as True, so when i is 5, while loop is still validating condition (i <= 5) as True, because i is not less than 5 but it is equal to 5 (At this point, i is 5 ), then while loop keeps running its body and executes line of code ( i += 1), adding one to i (now you got 6). After that, while loop stops running because condition (i <= 5 ) is not True anymore. THAT'S WHY WHEN I IS PRINTED, OUTPUT IS 6 AND NOT 5. You understand now?
To stop printing at 5, you would have to change the while loop from while i<=5: i+=1 print(i) to while i<=5: print(i) i+=1
David Ashton, now I understand, thank you very much. While is a conditional operator, and it checks each element for a condition, the number 6 fails the test and the loop stops, but it is printed, which is strange, like why print a number that failed the test.
Because there you are trying to print after adding 1 to i. So till 6 it will get printed. Try doing it like below https://code.sololearn.com/cFj4Zo8yf23n/?ref=app
Oumar abakar Abakar oumar What was the purpose of posting almost two dozen unrelated codes under the guise of helping? There was no point, and its just spam.
The loop doesn't stop at 5 because of the order of the commands within the loop. It currently flows like this: Set i value to 0 While i is less than or equal to 5 Add 1 to i Print i So it will print out 1,2,3,4,5,6 before it results in false. If you change the order to print i before adding 1 to it the results would be 0,1,2,3,4,5.
try to learn code with debbuger in VM like Visual Studio Code. then you can see what is doing step by step with your app.
When i=5 i<=5 will be true and enter into the loop , There it prints 6; Then loop ends.
Simba I know that, that's why I'm asking. why does the loop stop at the number 6 and not at number 5?
Hacker-KR4636, Simba Yes, I understand, Hacker-KR4636, your explanation is much more extensive and clear. But why in this code the variable i when equal to 5 does not pass the check again and does not print the expected result 6? https://code.sololearn.com/c3ck9wAiR79r/?ref=app
YEAH, VERY EXTENSIVE, BUT THIS ONE DEFEATS IT๐ When we count from 1 to 5, there are 5 numbers, but when we count from 0 to 5, there are 6 [ 0, 1, 2, 3, 4, 5, 6 ] You're getting the same result as your first code. Add this print function at end of your code and you will see that when while loop stops, i stores 6 as its value. (Here's how you can check what i'm saying, execute this program) i = 0 while i <= 5: print(i) i += 1 print(i) (At this point, i is equal to 5, but you need to add this line at end of your code and in that way you can see what's is the real i value after executing while loop) Outputs: 0 (This 0 is printed because you're using print(i) before ( i += 1), and value of i before ( i += 1) it's 0) 1 2 3 4 5 6 (this because when i is equal to 5, while keeps running because i is not less than 5 but so equal to 5) SORRY IF ANSWERS ARE EXTENSIVE I'M JUST TRYING TO HELP U ALL I CAN๐
The reason why 6 is printed is because i is incremented before printing . If you say inside the while loop ....: i = 0 while I<=5: print(i) i+=1 The result will end at 5 for the above piece