+ 2
Break and continue
Why this code counts 3 and then prints "continue" Shouldn't the output be like this? 1 2 Skipping 4 5 Python for begginers(26.1lesson) https://code.sololearn.com/cPhq9Gd845R3/?ref=app
7 Answers
+ 4
Mohammad Hasan
Do this
i = 0
while i < 5:
i += 1
if i == 3:
print("Skipping 3")
continue
print(i)
https://code.sololearn.com/cYxjZn39Wx2O/?ref=app
+ 4
You are first printing the value of i
1st iteration
Program print 1 than update the value . Value of i(2) is not equals to 3 so again checks the while condition and prints 2 then update the value now value of i is 3 if condition becomes true and skipping 3 is printed then continue(which has no use here)
Then the while condition is checked 3<=5 which is true so 3 get printed .
If you want the desired output -
i = 0
while i<=4:
i = i+1
if i == 3:
print("Skipping 3")
continue
print(i)
+ 2
This is because when i is equal to 3, "skipping 3" is printed and 3 is also printed. You haven't coded something that will not display 3 when i=3. I mean code something inside the if statement that will not display 3 and instead of that "skipping 3" will be printed.
I didn't know much about python, that why I am unable to help!
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
Thanks but what's the difference?
Yours worked but sololearn's didn't:/
+ 1
Mohammad Hasan
That's not a problem of Sololearn compiler, that's problem in your code.
You are 1st printing value then incrementing so after print 2 i will be 3 and now according to condition printing "Skipping 3" but i still hold 3 so 3 will be print then increment by 1
+ 1
Mohammad Hasan
I have already shared code in my first reply, you can check that.
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
No I didn't wrote this code it's in lessons
What do I need to do to fix that problem?