Python – How to use time.sleep() in milliseconds

How to use time.sleep() in milliseconds… here is a solution to the problem.

How to use time.sleep() in milliseconds

In my code, I have a variable called “time”.

I

set it to 2.0 (so that I don’t get an error when I subtract a float from an int).

I do time = time – 0.1

(so that when this line of code is repeated, it is shortened by 0.1 seconds each time).

But when I try to put it into time.sleep it doesn’t let me (because it’s decimal).

How can I subtract 100ms in milliseconds?

Look here for my code

Solution

You cannot call the variable time you use because the module that provides the sleep function has already called time. Therefore, your variable with the same name will make the module unusable.

Use different variable names.

Simple example:

import time
x=2.0
while x > 0:
    print(str(x), flush=True)
    time.sleep(x)
    x=x-0.1

Related Problems and Solutions