0
While loop decrement variable
Hi folks, When I modify the while loop to decrement value, it just shows nothing at all. Is there any trick that I am missing? Original code: i = 1 while i <=5: print(i) i = i + 1 Modified code: i = 5 while i <=1: print(i) i = i - 1
2 odpowiedzi
+ 14
Your 2nd while loop condition is false hence it does not run.
change it to something where it is true like below
i = 5
while i > 0 # will run till i is more than 0
print(i)
i -= 1 # shortcut for i = i - 1
Your 2nd code
i = 5
while i <= 1 # i will always be more than 1 hence did not run
print (i)
i -= 1
+ 1
Thanks for the reply.
I modified while statement to bigger than or equal (i>=1) and then it worked.
I got the logic behind my mistake. Thanks in advance.