Is there any way to end an infinite while loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Is there any way to end an infinite while loop?

Code: while 1==1: print("Never going to stop!") This program will never end. I am looking forward to your ideas about to stop it by adding timer or some other way? Is that possible?

16th May 2017, 12:33 AM
M_H
6 Answers
+ 4
Yep, there is a way. Decide what your stopping point is, and then use a break statement! That will snap you out of the while and on to whatever cafe is next.
16th May 2017, 12:44 AM
Jim
Jim - avatar
+ 4
Hmm, for that you might need to look up some of the libraries for python. I suspect there is a way to count seconds, but I don't know it offhand. The easy thing would be to count how many times you pass through the loop, but let's get more energetic. Look up the "time" library, I bet you can find a way to check if 5 seconds have passed
16th May 2017, 1:12 AM
Jim
Jim - avatar
+ 4
Here's another way to do it, using an outside timer to reset the condition: https://code.sololearn.com/cOKRiZ9ntR6P/?ref=app
17th May 2017, 1:45 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
https://code.sololearn.com/cyONBXWZfP8U/?ref=app
16th May 2017, 1:32 PM
Sapphire
0
@Jim thanks for the wonderful idea. Let's say, I want it to be stopped after 5 seconds. How to code that operation? Don't mind of my silly questions, I am a very new code learner and learning like ABCD.
16th May 2017, 1:03 AM
M_H
0
Thank you very much @Sapphire
16th May 2017, 11:36 PM
M_H