+ 14

Could anyone explain me the output?

i = 0 while 1==1: print(i) i = i +1 print("Finished") Output: 0 Finished 1 Finshed 2 Finished 3 Finished . . . So on.

9th Jul 2020, 10:20 AM
Sarfira
Sarfira - avatar
40 Réponses
+ 7
i is a variable with the value 0. while 1 == 1: is the same as while True: wich is an infinite loop (nothing tells it to stop) each time it loops it first prints the value of i, then adds 1 to the value of i, then prints finished. it will do this forever.
9th Jul 2020, 10:27 AM
Slick
Slick - avatar
+ 11
Do you think in both cases statement is always true?
9th Jul 2020, 10:55 AM
Sarfira
Sarfira - avatar
+ 11
Actually I was confused that why the Finished message also printing alternatively along with infinite loop.
9th Jul 2020, 11:00 AM
Sarfira
Sarfira - avatar
+ 8
1 == 1 will always be True, infinite loop. i = 0; Everytime it loops add 1 to i, means increment i by 1.
10th Jul 2020, 11:05 PM
BlackRose Mike
BlackRose Mike - avatar
+ 6
i assume you want it to print "finished" at the end but it will never do that since its an infinite loop. It's all about indentation in the loops try this: i = 0 while i < 6: print(i) i += 1 print('Finished')
9th Jul 2020, 10:36 AM
Slick
Slick - avatar
+ 5
Try to write break after print ("finish ") This will keep on counting till infinity but finish will print only once
9th Jul 2020, 11:04 AM
Bhavya gupta
Bhavya gupta - avatar
+ 3
"==" sign is a Boolean function to represent if a vale is true or false So you took i=0 In the loop you used 1 is true so it prints all the values and becomes an infinite loop
9th Jul 2020, 10:27 AM
Bhavya gupta
Bhavya gupta - avatar
+ 3
Like shift the print("finished ") Just below the inundation of while
9th Jul 2020, 10:38 AM
Bhavya gupta
Bhavya gupta - avatar
+ 3
He literally just said to do what that code i showed above does.
9th Jul 2020, 10:45 AM
Slick
Slick - avatar
+ 3
It ran exactly the way you programmed it. Look at the subtle differences between the code you posted and the code i posted. Copy and paste mine if it helps, you got it man. **Indentation**
9th Jul 2020, 10:56 AM
Slick
Slick - avatar
+ 3
So you set the variable i = 0 (I assume a counter) You then declared a while loop with the condition that it will run while 1 == 1 which is the same as saying while True As 1 is always going to be equal to 1 the loop will become an infinite loop (AKA never ending) There is only 1 way to exit this current loop, that would be with the break keyword which breaks out of the loop. Every iteration of this loop you are iterating your i variable and adding 1 to it. The best way to avoid an infinite loop is to set a boolean outside that will become true/ false at some point that will subsiquently exit the loop. Having either while 1==1 or while True is considered bad practice among a lot of python programmers. Heres an example. https://code.sololearn.com/cnWOSxeARIwd/?ref=app
9th Jul 2020, 11:07 PM
John Dem
John Dem - avatar
+ 3
i agree Vivek Vadi
10th Jul 2020, 4:02 AM
Jeremy Cheung
Jeremy Cheung - avatar
+ 2
I was confused that why "Finished" also printing alternatively along with the loop
9th Jul 2020, 10:32 AM
Sarfira
Sarfira - avatar
+ 2
Why do you disliking it? i am just clearing my doubt. There is nothing useless here. It's quite weird.
9th Jul 2020, 10:34 AM
Sarfira
Sarfira - avatar
+ 2
Finished is also placed in side the loop If you backspace the print command of finished then it won't print repeatedly
9th Jul 2020, 10:37 AM
Bhavya gupta
Bhavya gupta - avatar
+ 2
Bhavya gupta sorry i didn't get you. Tum hindi mein bhi samjha sakti ho
9th Jul 2020, 10:41 AM
Sarfira
Sarfira - avatar
+ 2
Slick let me try
9th Jul 2020, 10:43 AM
Sarfira
Sarfira - avatar
+ 2
What if we use 'break' statement before print("Finished")?
9th Jul 2020, 10:47 AM
Sarfira
Sarfira - avatar
+ 2
Give it a shot and see. Thats what the playground is for
9th Jul 2020, 10:48 AM
Slick
Slick - avatar
+ 2
Case 1: i = 0 while 1==1: print(i) i = i +1 break print("Finished") Output: 0 Finished Case2: i = 0 while i<6: print(i) i = i +1 print("Finished") Output: 0 Finished Both cases same output.
9th Jul 2020, 10:54 AM
Sarfira
Sarfira - avatar