3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DerpTrain
  • 69
  • 1
  • 1
  • 3
  • 1
    Your question is unclear. What is *2.0* supposed to mean? What does *When I call a certain function* mean? What is *time = time - 0.1* supposed to represent? What unit of time are you using? We can't read your mind. – Ken White Jan 07 '18 at 04:19
  • 1
    You should post here actual code and the error it is producing. – rnso Jan 07 '18 at 04:31
  • 2.0 is just so that when I subtract 0.1 from time that it won't give me an error for subtracting a float from an int – DerpTrain Jan 07 '18 at 04:37
  • And about when I call a certain function was just so that I didn't make it to personal. I have been persecuted for saying stuff like that before, I didn't want to make another mistake. – DerpTrain Jan 07 '18 at 04:38
  • 1
    see here: https://stackoverflow.com/questions/377454/how-do-i-get-my-python-program-to-sleep-for-50-milliseconds – keramat Jan 07 '18 at 04:39
  • Except of overwriting the time module, you are also trying to update it from a function name space without using `global`. – Klaus D. Jan 07 '18 at 05:19

2 Answers2

2

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
NineBerry
  • 26,306
  • 3
  • 62
  • 93
1

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

nomore
  • 41
  • 5