import time
def timer():
now = time.localtime(time.time())
return now[5]
run = raw_input("Start? > ")
while run == "start":
minutes = 0
current_sec = timer()
#print current_sec
if current_sec == 59:
mins = minutes + 1
print ">>>>>>>>>>>>>>>>>>>>>", mins
I want to create a kind of stopwatch that when minutes reach 20 minutes, brings up a dialog box, The dialog box is not the problem. But my minutes variable does not increment in this code.
Your code's perfect except that you must do the following replacement:
or
but here's another solution to this problem:
I was actually looking for a timer myself and your code seems to work, the probable reason for your minutes not being counted is that when you say that
and then
it is the same as saying
I'm betting that every time you print mins it shows you "1" because of what i just explained, "0+1" will always result in "1".
What you have to do first is place your
declaration outside of your while loop. After that you can delete the
line because you don't really need another variable in this case, just replace it with
That way minutes will start off with a value of "0", receive the new value of "0+1", receive the new value of "1+1", receive the new value of "2+1", etc.
I realize that a lot of people answered it already but i thought it would help out more, learning wise, if you could see where you made a mistake and try to fix it.Hope it helped. Also, thanks for the timer.
See Timer Objects from threading.
How about
Should you want pass arguments to the
timeout
function, you can give them in the timer constructor:Or you can use
functools.partial
to create a bound function, or you can pass in an instance-bound method.I want to create a kind of stopwatch that when minutes reach 20 minutes, brings up a dialog box.
All you need is to sleep the specified time. time.sleep() takes seconds to sleep, so 20 * 60 is 20 minutes.
I'd use a
timedelta
object.