0
while script exercise
My task is to "create a loop that increments the value of x by 2 and prints the even values whereas odd values print "not even"; however after some experimentation I cannot print the "not even" outputs. Any suggestions? x=0 while x<=20: if x % 2 == 0: print(x) x += 2 elif x % 2 != 0: print("not even")
3 Answers
+ 1
because you never have x as an odd number. your adding 2 each iteration. change line 5 to be x+=1 and add an increment to the elif condition.
x=0
while x<=20:
if x % 2 == 0:
print(x)
x += 1
elif x % 2 != 0:
print("not even")
x+=1
+ 1
if you increase by 2 you won't get an odd number. And also you need to put x +=2 outside if but still inside while
+ 1
a few more tries and found this to work:
x=0
while x<=20:
x = x + 1
if x % 2 != 0:
print("not even")
elif x % 2 == 0:
print(x)
Thanks Ranj and Forge for the suggestions!