Xyds
Xyds

Reputation: 3

A way to create a time delay without the use of time.sleep() in Python?

I'm fairly new to coding in general. This is for an assignment and I'm only allowed to use os and datetime module so yeah. Just wanna replicate the effect of this: (But without the use of time.sleep())

import time
def _loader(s):
    while s > 0:
        time.sleep(0.33)
        print(" . ", end="")
        s = s - 1
    time.sleep(0.33)
    print("\n")

I've tried this but it just produces an error when i try to subtract the two

from datetime import timedelta
from datetime import datetime
def _loader(s):
    while s > 0:
        t1 = timedelta(datetime.now())
        t2 = timedelta(seconds=0.33)
        while 1 == 1:
            t1temp = timedelta(datetime.now())
            if t1temp - t1 < t2:
                continue
            else:
                break
        print(" . ", end="")
        s = s - 1

Upvotes: 0

Views: 340

Answers (1)

Jayvee
Jayvee

Reputation: 10873

It seems that the problem is in the use of datetime.now() Try it in this way:

from datetime import timedelta
from datetime import datetime
def _loader(s):
    while s > 0:
        t1 = datetime.now()
        t2 = timedelta(seconds=0.33)
        while 1 == 1:
            t1temp = datetime.now()
            if t1temp - t1 < t2:
                continue
            else:
                break
        print(" . ", end="")
        s = s - 1

Upvotes: 1

Related Questions