40 AnswersNew Answer
40 AnswersNew Answer
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.
Do you think in both cases statement is always true?
Actually I was confused that why the Finished message also printing alternatively along with infinite loop.
Learner If you wanna make infinite loop , no need to write 1==1 Directly use while True: OR while 1:
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')
Try to write break after print ("finish ") This will keep on counting till infinity but finish will print only once
1 == 1 will always be True, infinite loop. i = 0; Everytime it loops add 1 to i, means increment i by 1.
"==" 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
Like shift the print("finished ") Just below the inundation of while
He literally just said to do what that code i showed above does.
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**
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
i agree Vivek Vadi
I was confused that why "Finished" also printing alternatively along with the loop
Why do you disliking it? i am just clearing my doubt. There is nothing useless here. It's quite weird.
Finished is also placed in side the loop If you backspace the print command of finished then it won't print repeatedly
Bhavya gupta sorry i didn't get you. Tum hindi mein bhi samjha sakti ho
Slick let me try
What if we use 'break' statement before print("Finished")?
Give it a shot and see. Thats what the playground is for
Learn Playing. Play Learning
Send us a message