복붙노트

[PYTHON] 파이썬에서 타이머 만들기

PYTHON

파이썬에서 타이머 만들기

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

스톱워치의 종류를 만들고 싶습니다. 분이 20 분이되면 대화 상자가 열립니다.이 대화 상자는 문제가 아닙니다. 하지만이 코드에서는 분 변수가 증가하지 않습니다.

해결법

  1. ==============================

    1.time.sleep을 사용하여이 전체 프로그램을 정말 단순화 할 수 있습니다.

    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 ">>>>>>>>>>>>>>>>>>>>>", mins
            # Sleep for a minute
            time.sleep(60)
            # Increment the minute total
            mins += 1
        # Bring up the dialog box here
    
  2. ==============================

    2.스레딩에서 타이머 객체를 참조하십시오.

    스레딩에서 타이머 객체를 참조하십시오.

    어때?

    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()
    

    인수를 timeout 함수에 전달하려면 타이머 생성자에서 인수를 전달할 수 있습니다.

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

    또는 functools.partial을 사용하여 바운드 함수를 만들거나 인스턴스에 바인딩 된 메서드를 전달할 수 있습니다.

  3. ==============================

    3.나는 timedelta 객체를 사용할 것이다.

    나는 timedelta 객체를 사용할 것이다.

    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
    
  4. ==============================

    4.다음 대체 작업을 수행해야한다는 것을 제외하고 코드가 완벽합니다.

    다음 대체 작업을 수행해야한다는 것을 제외하고 코드가 완벽합니다.

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

    또는

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

    그러나이 문제에 대한 또 다른 해결책이 있습니다 :

    def wait(time_in_seconds):
        time.sleep(time_in_seconds) #here it would be 1200 seconds (20 mins)
    
  5. ==============================

    5.

    mins = minutes + 1
    

    해야한다

    minutes = minutes + 1
    

    또한,

    minutes = 0
    

    while 루프 외부에 있어야합니다.

  6. ==============================

    6.스톱워치를 만들려하고 20 분이되면 몇 분 안에 대화 상자가 열립니다.

    스톱워치를 만들려하고 20 분이되면 몇 분 안에 대화 상자가 열립니다.

    필요한 시간은 잠자기 시간뿐입니다. time.sleep ()은 수면 시간이 2 초이므로 20 * 60은 20 분입니다.

    import time
    run = raw_input("Start? > ")
    time.sleep(20 * 60)
    your_code_to_bring_up_dialog_box()
    
  7. ==============================

    7.

    # 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 
    
  8. ==============================

    8.

    import time 
    
    ...
    
    def stopwatch(mins):
       # complete this whole code in some mins.
       time.sleep(60*mins)
    
    ...
    
  9. ==============================

    9.타이머 객체를 찾으려고합니다. http://docs.python.org/2/library/threading.html#timer-objects

    타이머 객체를 찾으려고합니다. http://docs.python.org/2/library/threading.html#timer-objects

  10. ==============================

    10.다음과 같이 while 루프를 사용해보십시오.

    다음과 같이 while 루프를 사용해보십시오.

    minutes = 0
    
    while run == "start":
       current_sec = timer()
       #print current_sec
       if current_sec == 59:
          minutes = minutes + 1
          print ">>>>>>>>>>>>>>>>>>>>>", mins
    
  11. ==============================

    11.

    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.
    
  12. ==============================

    12.실제로 타이머를 직접 찾고 있었고 코드가 작동하는 것처럼 보였습니다. 계산되지 않은 분에 대한 가능한 이유는 그렇게 말할 때입니다.

    실제로 타이머를 직접 찾고 있었고 코드가 작동하는 것처럼 보였습니다. 계산되지 않은 분에 대한 가능한 이유는 그렇게 말할 때입니다.

    그리고

    말하고있는 것과 같다.

    나는 분을 인쇄 할 때마다 방금 설명한대로 "1"을 보여줄 것이며, "0 + 1"은 항상 "1"이됩니다.

    먼저해야 할 일은

    while 루프 외부의 선언. 그 다음에

    왜냐하면이 경우에는 다른 변수가 필요 없기 때문입니다.

    그러면 분 "0"값으로 시작하고, "0 + 1"의 새 값을 받고, "1 + 1"의 새 값을 받고, "2 + 1"의 새 값을받습니다.

    많은 사람들이 이미 대답했지만, 실수로 어디에서 실수를하고 그것을 고치려 하는지를 알면 더 많은 것을 배우고, 현명하게 학습 할 수 있다고 생각했습니다. 도움이 되었기를 바랍니다. 타이머도 가져 주셔서 감사합니다.

  13. ==============================

    13.

    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)
    

    이 작업은 시간 초까지 아주 좋습니다.

  14. from https://stackoverflow.com/questions/18406165/creating-a-timer-in-python by cc-by-sa and MIT license