Skip a duplicate value when we use While condition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Skip a duplicate value when we use While condition

Syntax:- i = 1 while i<=5: print(i) i+=1 if i==3: print("Skipping 3") continue Output:- 1 2 Skipping 3 3 4 5 My doubt is though we mentioned it as print when i==3 as "Skipping 3" why does it pick 3 in next line? if we want to restrict such how to fix it?

17th Sep 2020, 6:25 PM
Abhi
Abhi - avatar
4 Answers
+ 1
Put print(i) and i+=1 after if block
17th Sep 2020, 6:44 PM
Abhay
Abhay - avatar
+ 1
Btw: Have you tried to use sets to eliminate dublicates? nums = [1,1,2,2,3] distinct_nums=set(nums) print(distinct_nums) >>> {1,2,3} If you realy need a list, you can then back cast with list() again. That should be rather fast, because its implemented in C.
17th Sep 2020, 11:32 PM
Zen Coding
Zen Coding - avatar
0
Add i+=1 before continue too
17th Sep 2020, 6:42 PM
Peter Parker
Peter Parker - avatar
0
continue just goes up the loop, so putting it at the bottom is pointless, continue isn't even needed here you can just increase i when i is 3 to avoid printing it: i = 1 while i <= 5: print(i) i += 1 if i == 3: i += 1 print("Skipping 3")
17th Sep 2020, 6:45 PM
Bagon
Bagon - avatar