Using for loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using for loop

x=10 i=0 while i>=6: if x%2==1: print(x) i=i+1 How to write this code with for loop and get the same output?

17th Nov 2020, 3:22 AM
Tanvir
Tanvir - avatar
5 Answers
+ 4
Brian Technically, your reason is the same as the one described by Sadvik B D. The solutions are different though. 😉
17th Nov 2020, 11:02 PM
David Carroll
David Carroll - avatar
+ 2
For the above code you won't get any output. Because your if condition become False in every loop. If you want same code in For loop means 👇👇👇👇. x=10 for i in range (7): If x%2==1: print (x) i=I+1
17th Nov 2020, 3:35 AM
Sadvik B D
Sadvik B D - avatar
+ 1
No need to increment i value manually inside for loop, Sadvik B D
24th Nov 2020, 12:54 PM
Nikhil Bk
Nikhil Bk - avatar
0
Adding to what Sadvik B D said, there is another reason that it will not produce output. It never enters the while loop because with i=0 the condition i>=6 is false. Did you mean i<=6?
17th Nov 2020, 8:39 PM
Brian
Brian - avatar
0
And I didn't find any use of this code Why are you using loop for 7 times and printing the same value(i.e x=10)?
24th Nov 2020, 12:58 PM
Nikhil Bk
Nikhil Bk - avatar