Creating a timer in python

后端 未结 13 2147
孤独总比滥情好
孤独总比滥情好 2020-12-07 18:58
import time
def timer():
   now = time.localtime(time.time())
   return now[5]


run = raw_input(\"Start? > \")
while run == \"start\":
   minutes = 0
   current_         


        
相关标签:
13条回答
  • 2020-12-07 19:21

    You're probably looking for a Timer object: http://docs.python.org/2/library/threading.html#timer-objects

    0 讨论(0)
  • 2020-12-07 19:25

    Your code's perfect except that you must do the following replacement:

    minutes += 1 #instead of mins = minutes + 1
    

    or

    minutes = minutes + 1 #instead of mins = minutes + 1
    

    but here's another solution to this problem:

    def wait(time_in_seconds):
        time.sleep(time_in_seconds) #here it would be 1200 seconds (20 mins)
    
    0 讨论(0)
  • 2020-12-07 19:28

    I'd use a timedelta object.

    from datetime import datetime, timedelta
    
    ...
    period = timedelta(minutes=1)
    next_time = datetime.now() + period
    minutes = 0
    while run == 'start':
        if next_time <= datetime.now():
            minutes += 1
            next_time += period
    
    0 讨论(0)
  • 2020-12-07 19:28
    import time 
    
    ...
    
    def stopwatch(mins):
       # complete this whole code in some mins.
       time.sleep(60*mins)
    
    ...
    
    0 讨论(0)
  • 2020-12-07 19:29
    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 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

    minutes = 0

    and then

    mins = minutes + 1

    it is the same as saying

    mins = 0 + 1

    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

    minutes = 0

    declaration outside of your while loop. After that you can delete the

    mins = minutes + 1

    line because you don't really need another variable in this case, just replace it with

    minutes = minutes + 1

    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.

    0 讨论(0)
  • 2020-12-07 19:31
    import time
    def timer(n):
        while n!=0:
            n=n-1
            time.sleep(n)#time.sleep(seconds) #here you can mention seconds according to your requirement.
            print "00 : ",n
    timer(30) #here you can change n according to your requirement.
    
    0 讨论(0)
提交回复
热议问题