0
What's wrong with my code?!
x = 9 while x < 10: print('x') x -1 == x if x % 2 == 1: print('x') continue I've written this code to print numbers 1,3,5,7,9 but it tells me that the "continue" is not in the loop... What's wrong with it?!
44 Answers
+ 21
Hi Atena!
That's because, the last line(x - 1 == x) of your updated code does nothing here. So, x is always < 10 hence it creates an infinite loop.
But it's supposed to decrease value of x to avoid giving this kind of output.
I assume that you're trying to make a code like this
x = 9
while x > 0:
if x % 2 == 1:
print(x)
x -= 1
+ 4
Because it is not necessary to equate x%2.
When x%2 returns 1 then the if condition become true and statements in if condition will execute. Similarly, if x%2 gives 0 then the if condition will be false and the else will execute
+ 3
To get the continue statement in the loop you have to indent it like the print statement above it.
But there are a lot of other issues with your code. You should take another look at the lessons.
+ 3
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner thank you so so much😌so the problem was actually in my second line... Thank you so much 🥰
+ 2
x = 9
while x>0:
if x%2:
print(x)
x-=1
+ 2
M. S. Why didn't you use == after your if statement? 🧐
+ 2
Ah, now I get it. Because python automatically treats 1 as True and 0 as False the result of the calculation is enough. Clever.
+ 2
Atena Molaie you can learn more about pep 8 from here ..
https://www.python.org/dev/peps/pep-0008/
+ 2
x = 1
while x != 9:
print(x)
x = x + 2
+ 2
For(int i = 0; i<=9; i++)
{
if(i%2 == 1)
print(i);
}
This will work I guess
+ 1
x = 9
while x < 10:
print(x)
x -1 == x
if x % 2 == 1:
print(x)
continue
Is this now ok?it still doesn't work though
+ 1
Atena Molaie run it in the code playground to find out.
+ 1
You have to put everything except the first line inside the while loop.
+ 1
M. S. I tried your code and it works. I was sure it would throw an error. Learned something new.
+ 1
Simon Sauter 😊😊
+ 1
M. S. Didn't know, thank you ☺️
Btw, didn't run the code, but seems like it's working
+ 1
Atena Molaie pleasure 😊
+ 1
My code work, but I have one mistake its 12 not 9.
+ 1
Гаврилов Егор I agree with you, but the author of the topic wanted to give 1 3 5 7 9
+ 1
Гаврилов Егор will you get 13579 even if you put x != 12 ??