Creating a timer in python

后端 未结 13 2149
孤独总比滥情好
孤独总比滥情好 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:35

    See Timer Objects from threading.

    How about

    from threading import Timer
    
    def timeout():
        print("Game over")
    
    # duration is in seconds
    t = Timer(20 * 60, timeout)
    t.start()
    
    # wait for time completion
    t.join()
    

    Should you want pass arguments to the timeout function, you can give them in the timer constructor:

    def timeout(foo, bar=None):
        print('The arguments were: foo: {}, bar: {}'.format(foo, bar))
    
    t = Timer(20 * 60, timeout, args=['something'], kwargs={'bar': 'else'})
    

    Or you can use functools.partial to create a bound function, or you can pass in an instance-bound method.

    0 讨论(0)
  • 2020-12-07 19:36
    import time
    mintt=input("How many seconds you want to time?:")
    timer=int(mintt)
    while (timer != 0 ):
        timer=timer-1
        time.sleep(1)
        print(timer)
    

    This work very good to time seconds.

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

    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.

    import time
    run = raw_input("Start? > ")
    time.sleep(20 * 60)
    your_code_to_bring_up_dialog_box()
    
    0 讨论(0)
  • 2020-12-07 19:41

    You can really simplify this whole program by using time.sleep:

    import time
    run = raw_input("Start? > ")
    mins = 0
    # Only run if the user types in "start"
    if run == "start":
        # Loop until we reach 20 minutes running
        while mins != 20:
            print(">>>>>>>>>>>>>>>>>>>>> {}".format(mins))
            # Sleep for a minute
            time.sleep(60)
            # Increment the minute total
            mins += 1
        # Bring up the dialog box here
    
    0 讨论(0)
  • 2020-12-07 19:42
    # this is kind of timer, stop after the input minute run out.    
    import time
    min=int(input('>>')) 
    while min>0:
        print min
        time.sleep(60) # every minute 
        min-=1  # take one minute 
    
    0 讨论(0)
  • 2020-12-07 19:42

    Try having your while loop like this:

    minutes = 0
    
    while run == "start":
       current_sec = timer()
       #print current_sec
       if current_sec == 59:
          minutes = minutes + 1
          print ">>>>>>>>>>>>>>>>>>>>>", mins
    
    0 讨论(0)
提交回复
热议问题