I am not sure if you meant that, but for me it seems like you just want to wait 2 seconds before executing the next steps. You can do that like so:
import time
while True:
time.sleep(2) # waits 2 seconds
winsound.Beep(440, 1000)
Anyways I don't recommend you to use a plain infinite loop, without a break statement. Therefore I recommend you to add one, like down below.
import time
while True:
time.sleep(2) # waits 2 seconds
winsound.Beep(440, 1000)
if True: # break on a specific statment
break
Edit: As CrazyChucky mentioned in the comments, this approach should work fine in most of the cases, but it can end up being more than two seconds sometimes. Therefore you should work with timedeltas or take a look at scheduler.
Answer from Maik Hasler on Stack OverflowI am not sure if you meant that, but for me it seems like you just want to wait 2 seconds before executing the next steps. You can do that like so:
import time
while True:
time.sleep(2) # waits 2 seconds
winsound.Beep(440, 1000)
Anyways I don't recommend you to use a plain infinite loop, without a break statement. Therefore I recommend you to add one, like down below.
import time
while True:
time.sleep(2) # waits 2 seconds
winsound.Beep(440, 1000)
if True: # break on a specific statment
break
Edit: As CrazyChucky mentioned in the comments, this approach should work fine in most of the cases, but it can end up being more than two seconds sometimes. Therefore you should work with timedeltas or take a look at scheduler.
To be more accurate as possible use:
import time
timer = 0
step = 2
t0 = time.time()
while True:
timer = time.time() - t0
wait = step - timer
time.sleep(wait)
print(time.time())
winsound.Beep(freq, duration)
t0 = time.time()
This script take in count the execution time of script lines for your computer.
Hello guys ! I’m starting with python a week ago, I don’t have all the knowledge etc…, but I’m very curious and I wish to know a method or how to make a Timer in python… like my timer start at 35min and decrease to 0 or to 0 to 35. If anybody have an idea how to do this, I'll take it !! See you ! 🐍
How To Use Timer With Loop in Python - Stack Overflow
time - Python loop to run for certain amount of seconds - Stack Overflow
How to Create a Loop That Runs Every 5 Minutes
Using a while loop and calling threading Timer?
Videos
while True: # True must be upper-case!
timer.start() # This is inside a loop so must be indented!
...
You'll note the error message you received tells you exactly that it needs to be indented.
import threading
from contextlib import closing
import serial
counter = 0
continue_looping = True
def stopper():
global continue_looping
continue_looping = False
timer = threading.Timer(15, stopper)
while (counter < 9 ):
timer.start()
with open("/Users/macproretina/Desktop/data.txt", 'w') as out_file:
with closing(serial.Serial('/dev/tty.usbmodem1411', 9600, timeout=1)) as ser:
while continue_looping:
line = ser.readline() # read a '\n' terminated line
out_file.write(line.decode('utf-8'))
out_file.flush()
counter = counter +1
"while (counter < 9 ): timer.start()"
just put a tab in fornd of "timer.start()" i think that should help
Try this:
import time
t_end = time.time() + 60 * 15
while time.time() < t_end:
# do whatever you do
This will run for 15 min x 60 s = 900 seconds.
Function time.time returns the current time in seconds since 1st Jan 1970. The value is in floating point, so you can even use it with sub-second precision. In the beginning the value t_end is calculated to be "now" + 15 minutes. The loop will run until the current time exceeds this preset ending time.
If I understand you, you can do it with a datetime.timedelta -
import datetime
endTime = datetime.datetime.now() + datetime.timedelta(minutes=15)
while True:
if datetime.datetime.now() >= endTime:
break
# Blah
# Blah
» pip install multitimer
Hi all,
I'm trying to write a function which says while the current time is less than the time limit, check if a file exists. Else, start the timer which the arguments are an interval, and the function itself to recheck for the file and join a 'timeout'.
However, it doesn't seem to like being in a while loop and spews an error message stating can't start another thread.
I've tried throwing in a cancel, but no luck.
My code:
t = Timer(check_intervals, watch_file) while time.time() < now + time_limit: if.os.path.exists(file): return True else: t.start() t.join(time_limit)
How can I refine this to work?
Thanks!