For loops

numbers = [1, 1, 2, 3]
for number in numbers:
if number % 2 == 0:
continue
print(number)

Can someone please explain why the answer would be 1, 1, 3?
I must be looking at this wrong. I thought the answer was 2 since it is the only number that would not leave a remainder and be equal to zero.

You are using continue in the conditional, not print(); you’re doing this backwards…

Instead of printing the number if it’s divisible by 2 (as you intend), you’re ignoring the number if it’s divisible by 2. You can correct this by removing the continue statement and placing your print() call into your conditional. All that are divisible by 2 will be printed, all that are not will be ignored (there will be no statements acting on them).

1 Like