How to use time.sleep() in milliseconds

11,249

Solution 1

You cannot call the variable you use time, because the module that supplies the sleep function is already called time. So your variable with the same name will make that module unavailable.

Use a different variable name.

Simple example:

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

Solution 2

Your code should work. The documentation also confirms subsecond precision is possible.

sleep(seconds) Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision. Type: builtin_function_or_method

Share:
11,249
DerpTrain
Author by

DerpTrain

Updated on June 27, 2022

Comments

  • DerpTrain
    DerpTrain almost 2 years

    In my code I have a variable called 'time'.

    I set it to 2.0 (so I do not get an error when I subtract a float from an int).

    I do time = time - 0.1 (this is so that when this line of code is repeated, it will be shorter by 0.1 seconds each time).

    But when I try and put it into time.sleep it won't let me (because it's a decimal)

    How can I do milliseconds instead so that I can just subtract 100 milliseconds instead?

    Look here for my code

  • Klaus D.
    Klaus D. over 6 years
    No semicolons in Python.