Q : Python time
<p>I would like to use Â·time()Â· to launch an event. An example would be to <code>print("test")</code> for 3 seconds. For that I did this: </p>

<pre><code>from time import time, sleep

from random import random

t = time()
n = 3

print(n, time() - t)

for i in range(100):
    sleep(0.04)
    print(time() - t)
    if time() - t &gt; n:
        print("test")
        break 
</code></pre>

<p>and it works! But in my game, in a while loop, it does not work... Why not?</p>

<p>If I've understood correctly, it seems you don't know how to run a simple gameloop and run some test code after 3 seconds, here's some naive approach:</p>

<pre><code>from time import time, sleep

from random import random

start_time = time()
n = 3

while True:
    elapsed_time = time() - start_time
    sleep(0.04)
    print(elapsed_time)
    if elapsed_time &gt; n:
        print("test")
        break
</code></pre>

python python-2.7 time