I am new in python I want to run my while loop for 300seconds or upto 300s After 300seconds it must terminate How I can write program in python Plz help.
Asked
Active
Viewed 264 times
-3
-
1You can easily do that with the sleep function of the time library, why using a loop? – diegoiva May 30 '18 at 18:32
-
1`time.sleep(300)`. What's your _real_ problem? – Jean-François Fabre May 30 '18 at 18:33
-
1What have you tried? Please post the code you are working on, it makes it a lot easier to help when you do. – Joshua Smith May 30 '18 at 18:33
-
@JoshuaSmith a useful "What have you tried" for once. – Jean-François Fabre May 30 '18 at 18:34
-
1Also, what do you mean exactly about the 300 secs? What if the content of the loop takes 299 seconds to run once? Checking the break criteria once per loop may cause it to run for 598 seconds. You've given no context on what you're trying to do. – roganjosh May 30 '18 at 18:37
-
I think you want to have something like a timer. Check out `time.time` in the official Python documentation. – Ziyad Edher May 30 '18 at 18:42
-
1Possible duplicate of [Python loop to run for certain amount of seconds](https://stackoverflow.com/questions/24374620/python-loop-to-run-for-certain-amount-of-seconds) – Thierry Lathuille May 30 '18 at 18:52
1 Answers
0
Try something like this, which will loop 300 seconds
import time
start_time = time.time()
while (time.time() - start_time) < 300:
print("/", end='')
print("End.")

Praveen Myakala
- 133
- 5