Reputation: 979
I am having a code where I have a for loop
I want that for loop run some specific time and then it stop or exit. I tried to find solutions like this:
import time
**NOT HELPFUL for me**
def stopwatch(seconds):
start = time.time()
time.clock()
elapsed = 0
while elapsed < seconds:
elapsed = time.time() - start
time.sleep(1)
stopwatch(20)
My code is like this:
for a in list:
if condition true:
print('Condition True')
else
print('not true')
So, i just need to run this loop for few seconds and then stop. Any help would be appreciated.
Upvotes: 1
Views: 60
Reputation: 813
Just subtract start time from current time (in seconds)
import time
start = time.time()
for a in list:
if time.time()-start > maxTimeout:
break
print("condition true" if condition else "not true")
Upvotes: 4