Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # alarm_clock.py
- from time import ctime, clock, sleep
- import winsound
- import sys
- import msvcrt
- def get_num_tadas_from_user():
- while True:
- n = input("\nHow many tadas?> ")
- if n == '':
- return 1
- else:
- try:
- n = int(n)
- return n
- except ValueError:
- continue
- def get_message_from_user():
- """
- The message is printed when the alarm rings.
- The idea is that the message will tell the user the reason
- for the alarm. Examples are, "Feed the cat", "Go to bed", "Write to Nick".
- """
- print("""
- Enter a message about what the alarm ringing means,
- in case you forget, or have several alarms set.
- """)
- message = input("\nMessage?> ")
- if message == '':
- return "no message"
- else:
- return message
- def get_alarm_time_from_user(num_tadas, message):
- while True:
- print("""
- Enter alarm time in 24-hour time.
- Both hours and minutes should be in 2 digits, separated by a colon.
- 13:00, 22:45, 02:30, 00:05
- Military time will be converted to 24-hour time
- 1300 -> 13:00, 0005 -> 00:05
- """)
- alarm_time = input("alarm time?> ")
- if ':' not in alarm_time:
- if len(alarm_time) == 4:
- alarm_time = alarm_time[:2] + ':' + alarm_time[2:]
- else:
- continue
- alarm_time_list = alarm_time.split(':')
- hour = alarm_time_list[0]
- if len(hour) != 2 or hour[0] not in '012' or hour[1] not in '0123456789':
- continue
- minute = alarm_time_list[1]
- if len(minute) != 2 or minute[0] not in '012345' or minute[1] not in '0123456789':
- continue
- print("\nIs", alarm_time, "the time you want? Hit enter for 'yes'; enter any character for 'no'")
- ans = input("\nanswer?> ")
- if ans == '':
- return alarm_time
- else:
- continue
- def today_or_tomorrow(alarm_time):
- now = ctime()[11:16]
- if time2seconds(now) > time2seconds(alarm_time):
- return " tomorrow"
- else:
- return " today"
- def tada_or_tadas(num_tadas):
- """
- Make "tada" plural when appropriate.
- "tada" is one of the sounds that come with Windows: taDAH.
- """
- if num_tadas == 1:
- return "tada"
- else:
- return "tadas"
- def time2seconds(time):
- """
- Given 24-hour time as string, return seconds as int.
- """
- time = time.replace(':', '')
- hour_string = time[:2]
- minute_string = time[2:]
- if hour_string[0] == '0':
- hour = int(hour_string[1])
- else:
- hour = int(hour_string)
- if minute_string[0] == '0':
- minute = int(minute_string[1])
- else:
- minute = int(minute_string)
- return 3600*hour + 60*minute
- def display_current_time():
- """
- Prints the current time as 24-hour time -- but no seconds.
- 13:07, 05:00, 23:59, 00:02, etc.
- Each printing overwrites the previous one.
- """
- sys.stdout.flush()
- print("The time now is", ctime()[11:16], "\r", end='')
- def print_summary(alarm_time, today_tomorrow, num_tadas, message):
- print("\nThe alarm is set to ring at ", alarm_time, today_tomorrow, ",", sep='')
- print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
- def ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas, message):
- print()
- while alarm_time != ctime()[11:16]:
- sleep(0.1)
- display_current_time()
- for tada in range(0, num_tadas):
- winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
- def main():
- print("\n" * 9)
- num_tadas = get_num_tadas_from_user()
- message = get_message_from_user()
- alarm_time = get_alarm_time_from_user(num_tadas, message)
- today_tomorrow = today_or_tomorrow(alarm_time)
- print_summary(alarm_time, today_tomorrow, num_tadas, message)
- ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas, message)
- print("\nmessage:", message, "\n")
- print()
- print("Hit Enter to close Alarm Clock")
- input()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment