Skip to content

Instantly share code, notes, and snippets.

@partrita
Last active June 22, 2025 04:59
Show Gist options
  • Select an option

  • Save partrita/158ba7a6bf11df3a91948102edf76caa to your computer and use it in GitHub Desktop.

Select an option

Save partrita/158ba7a6bf11df3a91948102edf76caa to your computer and use it in GitHub Desktop.
Simple timer by python code
import time
import sys
def stopwatch():
"""
간단한 터미널 기반 스톱워치를 실행합니다.
Ctrl+C를 눌러 스톱워치를 중지할 수 있습니다.
"""
start_time = time.time()
try:
while True:
elapsed_time = time.time() - start_time
# 시간을 분과 초로 계산
minutes = int(elapsed_time // 60)
seconds = int(elapsed_time % 60)
# 터미널에 시간 출력 (줄바꿈 없이 덮어쓰기)
sys.stdout.write(f"\r{minutes:02d}분 {seconds:02d}초")
sys.stdout.flush() # 버퍼 비우기
time.sleep(1) # 1초 대기
except KeyboardInterrupt:
# 사용자가 Ctrl+C를 눌렀을 때 실행
print("\n스톱워치 중지.")
final_elapsed_time = time.time() - start_time
final_minutes = int(final_elapsed_time // 60)
final_seconds = int(final_elapsed_time % 60)
print(f"총 경과 시간: {final_minutes:02d}분 {final_seconds:02d}초")
if __name__ == "__main__":
stopwatch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment