Home > Net >  print odd numbers in python
print odd numbers in python

Time:03-08

I want print odd numbers in range with Python with code below, but I don't understand what is wrong with it and only print 1 and stops.

a=1
while a<11:
    if a%2==0:
        continue
    print(a)
    a =1

CodePudding user response:

Her you go mate:

a=1
while a<11:
    if a%2!=0:
        print(a)
    a =1

CodePudding user response:

x = 1
while x < 15:
    if x % 2 != 0:
        print(x)
    x = x   1

This is how you do it. Main issue with your code is that continue skips everything, including the increment. Meaning your code causes an infinite loop. Continue skips all other lines in that iteration of a loop and goes to a new one, meaning a never increments meaning a is always less than 11.

CodePudding user response:

a=1
while a<11:
    if a%2==0:
        a =1
        continue
    print(a)
    a =1

You must increment a also when it's even, else it will be stuck in an infinite loop at a=2.

  • Related