0
Why would this code only print finished
Code i = 5 while i <=0: print(i) i -=1 print("Finished!")
12 odpowiedzi
+ 6
Use condition i >= 0, instead of i <= 0
+ 5
Joni ,
for further posts please keep in mind to mention the programming language in the tags.
+ 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..
+ 3
Because 5 is not less than 0
+ 3
Yes. Runs . but infinite times if you decrement instead of incrementing i..
+ 1
That's not the idea, I wanted to print from zero downwards
+ 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")}
+ 1
Instead of i<= 0 print i>=0
0
So A͢J ,let's say I = -10, the code will run?
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
0
bcz of the while condition as i<=0
so it won't enter the loop and print the next statement
0
Cause 5 is greater than 0 the condition will be false from the beginning so you need to make it i>=0