P
P
phawer-72021-08-16 12:44:31
Python
phawer-7, 2021-08-16 12:44:31

How to stop a while loop after time?

The while loop is started;
Within 1 minute writes something (print);
The cycle stops.

How to implement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KrimsN, 2021-08-16
@KrimsN

from time import time

seconds_to_work = 60  # Сколько выполнять цикл, в секундах
start = time()  # время начала выполнения
while time() - start < seconds_to_work: 
  print(...)

or
from time import time

seconds_to_work = 60  # Сколько выполнять цикл, в секундах
start = time()  # время начала выполнения
while True:
        if time() - start < seconds_to_work:
                break
  print(...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question