파이썬 05강. Chapter 10 설정시간 알리미
- 교재
- 컴퓨팅 사고와 함께하는 파이썬 기초부터 인공지능 응용까지
- 김성백, 김철민, 박잔정 저
- 연두에디션
- 문제 : 컴퓨터 작업에 집중하다 보면 시간 맞춰 해야 할 일(예 누군가에게 전화를 거는 일, 하던 작업을 그만 두고 다른 작업을 시작하는 일)들을 챙기지 못하게 되는 경우가 많다. 작업 진행 중에 수시로 시간을 체크할 솓 있겠지만 여간 번거로운 일이 아니다.
- 핵심요소 : print(), input(), int(), break, random() , time, winsound
- 소스 코드 1
12345678910111213141516171819import timeimport winsoundprint("[현재시각]", time.strftime("%p %I:%M:%S"))alarm_time = input("알람 시각 : ")alarm_hms = alarm_time.split(':')if len(alarm_hms) == 3 and 0 <= int(alarm_hms[0]) <= 23 \and 0 <= int(alarm_hms[1]) <= 59 and 0 <= int(alarm_hms[2]) <= 59:cur_hms = time.strftime("%H:%M:%S").split(':')sleep_secs = (int(alarm_hms[0]) - int(cur_hms[0])) * 60 * 60 \+ (int(alarm_hms[1]) - int(cur_hms[1])) * 60 + int(alarm_hms[2]) - int(cur_hms[2])if sleep_secs >= 0:time.sleep(sleep_secs)for i in range(1, 10):winsound.Beep(i * 100, 200)else:print("입력한 시각은 이미 지나갔습니다.")else:print("입력한 알람 시각 표기에 오류가 있습니다.") - 소스 코드 2
1234567891011121314151617181920212223242526import timecur_time = time.strftime("%H:%M:%S")cur_hms = cur_time.split(':')print("[현재시각]", cur_time)alarm_time = input("알람 시각 : ")alarm_hms = alarm_time.split(':')if len(alarm_hms) == 3 and 0 <= int(alarm_hms[0]) < 24 \and 0 <= int(alarm_hms[1]) < 60 and 0 <= int(alarm_hms[2]) < 60:sec = (int(alarm_hms[0]) - int(cur_hms[0])) * 3600sec += (int(alarm_hms[1]) - int(cur_hms[1])) * 60sec += (int(alarm_hms[2]) - int(cur_hms[2]))if sec > 0:time.sleep(sec)for i in range(1, 10):print('뻐꾹')time.sleep(1)else:print('알람 시각이 현재 시각 이전입니다.')else:print("입력한 알람 시각 표기에 오류가 있습니다.") - 과제1 : 세계 각국은 시간대에 따라 시간이 다르게 표시된다. 한국, 영국, 미국 서부의 시간을 동시에 출력하는 프로그램 만들기