1
import time
    
while True:
    start=time.time()
    time.sleep(2)
    end=time.time()
    howlong = end - start
    print(howlong)

I coded this so that I can have something happen 2 seconds later. the expected output is 2.00 yet the output varies between 2.00 and 2.02 on my pc. is there anything I can do so that I always have less than 2.01 every time? I haven’t tried anything since I’m a noob.

Edit: Never mind I switched to this:

while True:
    start = time.time()
    goal = start+2
    while True:
        end = time.time()
        if end >= goal:
            print(end-start)
            break

The results are more consistent.

Karma
  • 11
  • 2
  • as you are calculating 'howlong' after the sleep, any chance the delay is the time between? – Captain Caveman May 31 '22 at 17:37
  • well that's the issue, even if it was the time in between why does it vary? the time in between should be the same. so it should give me a consistent 2.01 or 2.02 not vary. – Karma Jun 02 '22 at 02:29

0 Answers0