+ 6

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

30th Nov 2020, 8:56 AM
😎_M_😎
😎_M_😎 - avatar
28 Respuestas
+ 25
Because 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.
30th Nov 2020, 9:12 AM
David Ashton
David Ashton - avatar
+ 10
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👍🏻
30th Nov 2020, 10:08 AM
Simba
Simba - avatar
+ 8
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?
30th Nov 2020, 9:48 AM
Hacker-KR4636
Hacker-KR4636 - avatar
+ 7
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
1st Dec 2020, 3:18 AM
David Ashton
David Ashton - avatar
+ 3
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
1st Dec 2020, 9:39 AM
Dr£@M_c@tcH£r
Dr£@M_c@tcH£r - avatar
+ 2
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.
30th Nov 2020, 9:21 AM
😎_M_😎
😎_M_😎 - avatar
+ 2
Remove the =
1st Dec 2020, 3:59 PM
Coder
Coder - avatar
+ 2
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.
1st Dec 2020, 9:15 PM
Aria
Aria - avatar
+ 2
U should use i<5 instead of i<=5
2nd Dec 2020, 6:04 AM
Namit Aggarwal
+ 2
Thanks for the answer David Ashton i learn something new
2nd Dec 2020, 7:18 AM
Jiya
Jiya - avatar
+ 1
i=0 while i<=4: i+=1 print(i) Second method i=0 while i<5: i+=1 print(i)
1st Dec 2020, 9:52 AM
AMAN DHIMAN
AMAN DHIMAN - avatar
+ 1
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.
1st Dec 2020, 2:14 PM
Tisagh Chase
Tisagh Chase - avatar
+ 1
Good
1st Dec 2020, 4:49 PM
Дмитрий Мандрин
Дмитрий Мандрин - avatar
+ 1
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.
1st Dec 2020, 4:53 PM
Radosław Krzysiu Krzyżagórski
Radosław Krzysiu Krzyżagórski - avatar
+ 1
You have to add i+=1 after the print function.
2nd Dec 2020, 4:38 AM
Mohammad
Mohammad - avatar
+ 1
When i=5 i<=5 will be true and enter into the loop , There it prints 6; Then loop ends.
2nd Dec 2020, 6:24 AM
Sandeep Matla
Sandeep Matla - avatar
0
Simba I know that, that's why I'm asking. why does the loop stop at the number 6 and not at number 5?
30th Nov 2020, 9:09 AM
😎_M_😎
😎_M_😎 - avatar
0
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
30th Nov 2020, 10:01 AM
😎_M_😎
😎_M_😎 - avatar
0
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😃
30th Nov 2020, 5:32 PM
Hacker-KR4636
Hacker-KR4636 - avatar
0
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
1st Dec 2020, 12:37 AM
Liberty Keith Gapare
Liberty Keith Gapare - avatar