+ 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?
6 Antworten
+ 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.
+ 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
+ 4
Here's another way to do it, using an outside timer to reset the condition:
https://code.sololearn.com/cOKRiZ9ntR6P/?ref=app
+ 1
https://code.sololearn.com/cyONBXWZfP8U/?ref=app
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.
0
Thank you very much @Sapphire