+ 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?
4 Respuestas
+ 1
Put print(i) and i+=1 after if block
+ 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.
0
Add i+=1 before continue too
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")