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?!

23rd Aug 2021, 1:12 PM
Atena Molaie
44 ответов
+ 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
23rd Aug 2021, 3:33 PM
Python Learner
Python Learner - avatar
+ 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
23rd Aug 2021, 5:20 PM
M. S.
M. S. - avatar
+ 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.
23rd Aug 2021, 1:17 PM
Simon Sauter
Simon Sauter - avatar
+ 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 🥰
23rd Aug 2021, 3:39 PM
Atena Molaie
+ 2
x = 9 while x>0: if x%2: print(x) x-=1
23rd Aug 2021, 4:43 PM
M. S.
M. S. - avatar
+ 2
M. S. Why didn't you use == after your if statement? 🧐
23rd Aug 2021, 5:17 PM
Atena Molaie
+ 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.
23rd Aug 2021, 5:26 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Atena Molaie you can learn more about pep 8 from here .. https://www.python.org/dev/peps/pep-0008/
24th Aug 2021, 3:49 AM
Sacar
Sacar - avatar
+ 2
x = 1 while x != 9: print(x) x = x + 2
24th Aug 2021, 6:33 AM
Гаврилов Егор
Гаврилов Егор - avatar
+ 2
For(int i = 0; i<=9; i++) { if(i%2 == 1) print(i); } This will work I guess
25th Aug 2021, 8:08 AM
GURURAJ KL
GURURAJ KL - avatar
+ 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
23rd Aug 2021, 1:30 PM
Atena Molaie
+ 1
Atena Molaie run it in the code playground to find out.
23rd Aug 2021, 1:31 PM
Simon Sauter
Simon Sauter - avatar
+ 1
You have to put everything except the first line inside the while loop.
23rd Aug 2021, 2:44 PM
Simon Sauter
Simon Sauter - avatar
+ 1
M. S. I tried your code and it works. I was sure it would throw an error. Learned something new.
23rd Aug 2021, 5:22 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Simon Sauter 😊😊
23rd Aug 2021, 5:23 PM
M. S.
M. S. - avatar
+ 1
M. S. Didn't know, thank you ☺️ Btw, didn't run the code, but seems like it's working
23rd Aug 2021, 5:29 PM
Atena Molaie
+ 1
Atena Molaie pleasure 😊
23rd Aug 2021, 5:30 PM
M. S.
M. S. - avatar
+ 1
My code work, but I have one mistake its 12 not 9.
24th Aug 2021, 6:44 AM
Гаврилов Егор
Гаврилов Егор - avatar
+ 1
Гаврилов Егор I agree with you, but the author of the topic wanted to give 1 3 5 7 9
24th Aug 2021, 6:51 AM
Sacar
Sacar - avatar
+ 1
Гаврилов Егор will you get 13579 even if you put x != 12 ??
24th Aug 2021, 6:55 AM
Sacar
Sacar - avatar