-1

I just need to know why the code generated numbers that are not divisible by 5.

for x in range (10,20):
            #if (x == 15): break
            if (x % 5 == 0) : continue
            print(x)

Is my understanding correct, that x % 5 == 0 means all x values that have a remainder of 0 when divided by 5? So would it mean that the code should generate all values that are divisible by 5?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 1
    `continue` means "_stop the current iteration and start the next one_". when you find a number divisible by 5, you do _not_ print it. – DYZ Jul 01 '20 at 04:05
  • Your understanding of `x % 5 == 0` is correct, but you should be printing those `x` instead of `continue`-ing. – Gino Mempin Jul 01 '20 at 04:16

2 Answers2

0

Your understanding of the x % 5 == 0 is correct. It is True is x is a multiple of 5 and False if not.

continue is a keyword used in order to stop the current iteration there and go to the next iteration now.

The code you're looking for looks like that:

for x in range(10, 20):
    if x % 5 == 0:
        print(x)

or

for x in range(10, 20):
    if x % 5 != 0:
        continue
    print(x)
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Sohibnut
  • 11
  • 1
0

The continue statement skips the rest of the code block and continues the for loop with the next item in the iteration. Rather, change if (x % 5 == 0) : to if (x % 5 != 0) :.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76