Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Alarm Clock v2.0
- from time import ctime, clock, sleep
- import winsound
- import sys
- def tadas_time(n):
- return round((int(n)-1) * 1.79 + 1, 1)
- def get_num_tadas_from_user():
- try:
- print("Enter the number of taDAHs desired; Entering nothing will set the number to 1")
- n = input("\nHow many tadas?> ")
- if not n:
- print("The number of taDAHs was set to 1")
- return 1
- if int(n) < 1:
- print("The number of taDAHs must be at least 1, or the alarm won't sound")
- raise ValueError
- if int(n) > 3:
- print("Are you sure you want", n, "taDAHs? That will take", tadas_time(n), "seconds")
- print("Hit enter for 'yes'; enter any character if you want to change the number")
- ans = input("\nanswer?> ")
- if ans == '':
- print("The number of taDAHs was set to", n)
- return int(n)
- else:
- raise ValueError
- print("The number of taDAHs was set to", n)
- return int(n)
- except ValueError:
- return get_num_tadas_from_user()
- 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.
- Enter nothing if no message.
- """)
- message = input("\nMessage?> ")
- if message == '':
- print("no message entered")
- return None
- else:
- return message
- def get_alarm_time_from_user(flag):
- if flag:
- print("""
- Enter an 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?> ")
- try:
- # The user uses something other than a colon (say a semicolon)
- # to separate hour and minute
- if len(alarm_time) == 5 and alarm_time[2] != ':':
- print("A colon should come between hours and minutes: 10:45.\n")
- raise ValueError
- if ':' not in alarm_time:
- if len(alarm_time) != 4:
- print("Military time should have exactly 4 digits.\n")
- raise ValueError
- if not alarm_time.isdigit():
- print("There's a non-digit in either the hours or minutes or both.\n")
- raise ValueError
- alarm_time = alarm_time[:2] + ':' + alarm_time[2:]
- if not (alarm_time.replace(':', '').isdigit()):
- print("You've put a non-digit in either the hours or minutes or both.\n")
- raise ValueError
- hour, minute = alarm_time.split(':')
- if len(hour) != 2 or len(minute) != 2:
- print("Both hours and minutes should have exactly 2 digits.\n")
- raise ValueError
- if not 0 <= int(hour) < 24:
- print("Hours should be >= 0 and less than 24.\n")
- raise ValueError
- if not 0 <= int(minute) < 60:
- print("Minutes should be >= 0 and less than 60.\n")
- raise ValueError
- 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:
- flag = False
- return get_alarm_time_from_user(flag)
- return alarm_time
- except ValueError:
- flag = False
- return get_alarm_time_from_user(flag)
- 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 "taDAH"
- else:
- return "taDAHs"
- def time2seconds(alarm_time):
- """time_string format = hh:mm, returns seconds as int."""
- hour, minute = alarm_time.split(':')
- return int(hour)*3600 + int(minute)*60
- def today_or_tomorrow(alarm_time):
- now = ctime()[11:16]
- if time2seconds(now) > time2seconds(alarm_time):
- return "tomorrow"
- else:
- return "today"
- def print_summary(alarm_time, today_tomorrow, num_tadas, message):
- print("\nThe alarm is set to ring at", alarm_time, today_tomorrow)
- if message:
- print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
- else:
- print("with", num_tadas, tada_or_tadas(num_tadas), "and no message.")
- 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 main():
- # the flag will prevent the user being repeated shown all the instructions about entering alarm time.
- flag = True
- print("\n" * 9)
- # Get input from user.
- num_tadas = get_num_tadas_from_user()
- message = get_message_from_user()
- alarm_time = get_alarm_time_from_user(flag)
- # Print summary of alarm info.
- today_tomorrow = today_or_tomorrow(alarm_time)
- print_summary(alarm_time, today_tomorrow, num_tadas, message)
- # Main loop (wait until alarm time).
- print()
- while alarm_time != ctime()[11:16]:
- sleep(0.1)
- display_current_time()
- # Sound alarm
- for tada in range(0, num_tadas):
- winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
- # Display alarm message and close.
- if 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