본문 바로가기
파이썬 공부중

[Python] 카운트다운 코드

by 그믐치 2021. 5. 24.

import time

def countdown(time_sec):
    while time_sec:
        mins, secs = divmod(time_sec, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='\r')
        time.sleep(1)
        time_sec -= 1

    print("stop")

countdown(120)

divmod()

두 인자의 나눈 몫과 나머지 값을 튜플로 반환한다.

 

print(timeformat, end='\r')

출력물을 나열시키지 않고 같은 자리에 덥어쓴다.

 

노트패드++을 사용하면 출력 콘솔창에 덥어쓰기하는 과정이 안보이기때문에 카운트과정을 볼 수 없으므로 도스창에서 실행해야 함. 파이참에서는 정상적으로 카운트를 보여줌.

댓글