DerpTrain
DerpTrain

Reputation: 69

How to use time.sleep() in milliseconds

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

Upvotes: 3

Views: 17217

Answers (2)

NineBerry
NineBerry

Reputation: 28499

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

Upvotes: 2

nomore
nomore

Reputation: 41

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

Upvotes: 1

Related Questions