2

How should I measure number of seconds passed in a infinite loop? I'm using Vpython, and in my loop i change a position of some elements etc. The thing is that i have to change color of lights(sphere) every X seconds and it all happens in infinite loop.

while True: #some stuff 'here i have to count those seconds' #some other stuff 

2 Answers 2

3

You could determine the elapsed time by calling time.time(), and taking the difference.

i have to change color of lights(sphere) every X seconds

Say you last changed the colour at time T. Keep calling time.time() inside the loop, and change the colour again when time.time() - T exceeds X.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, but with what function i should initiate the first time?
It works now, I'll try though mgilson way as well and see which one is better.
2

You should probably use timeit.default_timer() to figure out what time it is and compute the difference.

e.g.:

from timeit import default_timer clock_start = default_timer() #... do something here clock_end = default_timer() print "This took %s seconds" %( clock_end - clock_start ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.