Here is my goal: To make a small program (text based) that will start with a greeting, print out a timer for how long it has been since the last event, and then a timer for the
# Timer
import time
import winsound
print " TIMER"
#Ask for Duration
Dur1 = input("How many hours? : ")
Dur2 = input("How many minutes?: ")
Dur3 = input("How many seconds?: ")
TDur = Dur1 * 60 * 60 + Dur2 * 60 + Dur3
# Ask to Begin
start = raw_input("Would you like to begin Timing? (y/n): ")
if start == "y":
timeLoop = True
# Variables to keep track and display
CSec = 0
Sec = 0
Min = 0
Hour = 0
# Begin Process
timeLoop = start
while timeLoop:
CSec += 1
Sec += 1
print(str(Hour) + " Hours " + str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
Hour = 0
print(str(Min) + " Minute(s)")
if Min == 60:
Sec = 0
Min = 0
Hour += 1
print(str(Hour) + " Hour(s)")
elif CSec == TDur:
timeLoop = False
print("time\'s up")
input("")
while 1 == 1:
frequency = 1900 # Set Frequency To 2500 Hertz
duration = 1000 # Set Duration To 1000 ms == 1 second
winsound.Beep(frequency, duration)
I based my timer on user5556486's version. You can set the duration, and it will beep after said duration ended, similar to Force Fighter's version
a simple timer program that has sound to remind you would be:
from time import sleep
import winsound
m = 0
print("""**************************
Welcome To FASTIMER®
**************************""")
while True:
try:
countdown = int(input("How many seconds: "))
break
except ValueError:
print("ERROR, TRY AGAIN")
original = countdown
while countdown >= 60:
countdown -= 60
m += 1
for i in range (original,0,-1):
if m < 0:
break
for i in range(countdown,-2,-1):
if i % 60 == 0:
m-=1
if i == 0:
break
print(m," minutes and ",i," seconds")
sleep(1)
if m < 0:
break
for j in range(59,-1,-1):
if j % 60 == 0:
m-=1
print(m," minutes and ",j," seconds")
sleep(1)
print("TIMER FINISHED")
winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
this program uses time.sleep() to wait a second. It converts every 60 seconds to a minute. the sound only works with Windows or you can install pygame to add sounds.
On my PC (Windows 7) when run in a cmd
window, this program works almost exactly as you say it should. If the timer is repeating on a new line with each second, that suggests to me that os.system ('cls')
is not working for you -- perhaps because you're running on an OS other than Windows?
The statement while s<=60:
appears to be incorrect because s
will never be equal to 60 in that test -- anytime it gets to 60, it is reset to 0 and m
is incremented. Perhaps the test should be while m<60:
?
Finally, on my PC, the timer does not appear to lag behind actual seconds on the clock by much. Inevitably, this code will lag seconds on the clock by a little -- i.e. however long it takes to run all the lines of code in the while
loop apart from time.sleep(1)
, plus any delay in returning the process from the sleeping state. In my case, that isn't very long at all but, if running that code took (for some reason) 0.1 seconds (for instance), the timer would end up running 10% slow compared to wall clock time. @sberry's answer provides one way to deal with this problem.
This is my version. It's great for beginners.
# Timer
import time
print("This is the timer")
# Ask to Begin
start = input("Would you like to begin Timing? (y/n): ")
if start == "y":
timeLoop = True
# Variables to keep track and display
Sec = 0
Min = 0
# Begin Process
timeLoop = start
while timeLoop:
Sec += 1
print(str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
print(str(Min) + " Minute")
# Program will cancel when user presses X button
I would go with something like this:
import time
import sys
time_start = time.time()
seconds = 0
minutes = 0
while True:
try:
sys.stdout.write("\r{minutes} Minutes {seconds} Seconds".format(minutes=minutes, seconds=seconds))
sys.stdout.flush()
time.sleep(1)
seconds = int(time.time() - time_start) - minutes * 60
if seconds >= 60:
minutes += 1
seconds = 0
except KeyboardInterrupt, e:
break
Here I am relying on actual time module rather than just sleep incrementer since sleep won't be exactly 1 second.
Also, you can probably use print
instead of sys.stdout.write
, but you will almost certainly need sys.stdout.flush
still.
Like:
print ("\r{minutes} Minutes {seconds} Seconds".format(minutes=minutes, seconds=seconds)),
Note the trailing comma so a new line is not printed.