For a project i'm working on now, i need to create a function that is in the format Hours:Minutes: seconds: Fraction

where fraction is 1/10th of a second.

It should print out every one tenth of a second. I have no idea where to start or how to do this, and am fairly new to python.

it has to be as simple as possible and use time and time.sleep. I know time.sleep has to be time.sleep(.1), and to import time, but I dont know how to use nested loops with this.

Some help would be greatly appreciated :)

All 9 Replies

Well start by allowing the user to input how much time they want to count down. Convert each section (hours, mins etc.) into an int so that you can count it down. Count down by 0.1 seconds (use ifs to find if one column equals 0, in which case it will become 9, and take 1 off the next column), convert the numbers back into a string, print, sleep (0.1). Repeat until all sections equal zero. Seeing as it takes a few milliseconds to execute each line of code, you may want to correlate your timer with real time. Consider using a separate module (maybe datetime?) to get the current time, and then you can synchronise your timer with what it should be to prevent it going out of step. This should only need to be done every few seconds.

I actually just need it to count from 00:00:00:0 to an infinite number, with it printing it out at every .1 second. So I guess that reverses the number count?

def timer_tenth_second():
import time
hh = 0
mm = 0
ss = 0
ff = 0
counter=ff
while True:
counter+= 1
if counter > 9:
counter = 0
if counter < 1:
ss= ss + 1
if ss > 59:
print(hh, + ":", + mm, + ":", + ss, + ":", + str(ff))
time.sleep(.1)
timer_tenth_second()

Here is my current code, help ?

try this url
http://www.daniweb.com/code/snippet216475.html

I think you should use a combination of an infinite loop with the time.clock() function but after dividing it to .1 seconds seconds minutes and hours. :)

Well it's the same strategy. You still need to count up with integers and convert to a string at the end. You should only need one loop for all that. Don't forget your ifs for all the columns as in my above post. If you want to keep it simple just have the loop with time.sleep(), but again you could sync it with real time for more accuracy.

``````import time
hh = 0
mm = 0
ss = 0
ff = 0
while True:
ff = ff + 1
if ff > 9:
ff = -1
if ff == -1:
ff = 0
ss= ss + 1
if ss > 59:
ss = -1
if ss == -1:
ss = 0
mm = mm + 1
if mm > 59:
mm = -1
if mm == -1:
mm = 0
hh = hh + 1
print hh, ":", mm, ":", ss, ":", ff
time.sleep(.1)``````

I'm testing it and so far it's working fine

Please mark the thread solved if your problem has been fixed.

This is little bit cleaner code than posted before:

``````from __future__ import print_function, division
import time
counter, timestr = 0, ''

print(time.asctime())
t0 = time.time()
try:
while True:
sectime, ff = divmod(counter,10)
mintime, ss = divmod(sectime,60)
hh, mm = divmod(mintime, 60)
print(''.join('\b' for c in timestr), end='')
timestr='%02i:%02i:%02i.%1s' % (hh, mm, ss, ff)
print(timestr, end='')
time.sleep(.1)
counter += 1
except KeyboardInterrupt:
t0 = time.time() - t0
print('\nCounter seconds: %.1f, time.time seconds: %.1f'
% (counter/10.0, t0 ))``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.