Reputation: 11
I want to print the numbers from 1 to 100 and skip the numbers divisible by 3 or 5. I made an indentation mistake I got two values. What is the logic?
Here is the code:
i = 1
while i<=100:
if not(i%3==0 or i%5==0):
print(i,end=" ")
i=i+1 ***indentation mistake***
Upvotes: 0
Views: 1473
Reputation: 2118
@Thymen Already explained it very well in the comments, but basically if not (i%3==0 or i%5==0):
will mean that if the number is divisible by either 3 or 5 then of course the code inside the if
block will not be executed. This means that as soon as i=3
, the if
block will be skipped and the while loop will continue, without having incremented i
, which leads to an infinite loop.
As a side note I would write this with a continue
statement so it's clear what you're doing:
i = 0
while i < 100:
i += 1
if not i % 3 or not i % 5:
continue
print(i, end=" ")
Upvotes: 2