Why would this code only print finished | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why would this code only print finished

Code i = 5 while i <=0: print(i) i -=1 print("Finished!")

12th Jan 2023, 9:22 AM
Jonathan
Jonathan - avatar
12 Answers
+ 6
Use condition i >= 0, instead of i <= 0
12th Jan 2023, 9:43 AM
Jayakrishna 🇮🇳
+ 5
Joni , for further posts please keep in mind to mention the programming language in the tags.
12th Jan 2023, 10:29 AM
Lothar
Lothar - avatar
+ 3
Yes. It prints downwards only ... with i>=0 outputs : 543210 Check this output in playground.. But your condition I<=0 => 5<=0 false, so loop won't start..
12th Jan 2023, 9:52 AM
Jayakrishna 🇮🇳
+ 3
Because 5 is not less than 0
12th Jan 2023, 12:36 PM
A͢J
A͢J - avatar
+ 3
Yes. Runs . but infinite times if you decrement instead of incrementing i..
12th Jan 2023, 12:52 PM
Jayakrishna 🇮🇳
+ 1
That's not the idea, I wanted to print from zero downwards
12th Jan 2023, 9:48 AM
Jonathan
Jonathan - avatar
+ 1
Your condition is wrong in the while loop(i<=0). And if the condition is wrong then the compiler will not go inside the while loop and it will leave the while loop part and simply go to the next part which is {print("Finished")}
13th Jan 2023, 3:16 AM
Amrit Gupta
Amrit Gupta - avatar
+ 1
Instead of i<= 0 print i>=0
13th Jan 2023, 4:01 PM
VISHNUPRIYA S
VISHNUPRIYA S - avatar
0
So A͢J ,let's say I = -10, the code will run?
12th Jan 2023, 12:46 PM
Jonathan
Jonathan - avatar
0
In your code the "i" will only be printed if the value of "i" are below 0. You should change from . while i <= 0: print(i) i -= 1 . To this while i >= 0: print(i) i -= 1
13th Jan 2023, 3:39 PM
Tian Tama
Tian Tama - avatar
0
bcz of the while condition as i<=0 so it won't enter the loop and print the next statement
13th Jan 2023, 6:19 PM
Rahul
0
Cause 5 is greater than 0 the condition will be false from the beginning so you need to make it i>=0
13th Jan 2023, 7:53 PM
MuqtadaNuri
MuqtadaNuri - avatar