Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Alarm Clock 4.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:
- # recursion
- 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 find_alpha(at):
- for char in at:
- if char.isalpha():
- return char
- return None
- def get_alarm_time():
- alarm_time = input("alarm time?> ")
- at = alarm_time
- try:
- # User hits Enter before typing his alarm time
- if not at:
- raise ValueError
- # User thinks that hours and minutes should be separated by a space
- if ' ' in at:
- print("Do not use a space anywhere in alarm time.")
- print("Use a colon to separate hours and minutes: 10:45, 03:07")
- print("Or use military time: 0915, 2115, 0003, 1203\n")
- raise ValueError
- # User somehow thinks alarm time should or could be negative
- if at[0] == '-':
- print("alarm time cannot be negative.\n")
- raise ValueError
- # User enters +3:00. Possibly thinking he wants the alarm to ring 3 hours from now?\n"
- if '+' in at:
- print("Do not use '+' anywhere in alarm time.\n")
- raise ValueError
- #User thinks he should use 'l' for '1'.
- if 'l' in at:
- print("You've used an 'l' (lower case 'L') in your alarm time instead of the digit '1' (one).\n")
- raise ValueError
- # User enters 1o45 or ooo3
- if 'o' in at:
- print("You've used the letter 'o' in your alarm time instead of the digit '0' (zero).\n")
- raise ValueError
- # User enters 1O45 or OOO3
- if 'O' in at:
- print("You've used the letter 'O' in your alarm time instead of the digit '0' (zero).\n")
- raise ValueError
- # User uses an alphabetic letter other than the above o, O, or l
- char = find_alpha(at)
- if char:
- print("Do not use an alphabetic letter such as '", char, "' in alarm time.\n", sep='')
- raise ValueError
- if len(at) == 5:
- # User enters 12345
- if at.isdigit():
- print("Military time should have exactly 4 digits, not 5.")
- print("0915, 2115, 0003, 1203\n")
- raise ValueError
- # User enters 10;45
- if at[2] == ';' and at.replace(';', '').isdigit():
- print("You've used a semicolon between hours and minutes. Use a colon: 10:45, 03:07.\n")
- raise ValueError
- # User enters 10,45 or 10.45 or 10/45, or 10x45, etc.
- if at[2] != ':' and at.replace(at[2], '').isdigit():
- print("Use a colon between hours and minutes: 10:45, 03:07\n")
- raise ValueError
- if ':' not in at:
- # User enters 123 or 123456
- if len(at) != 4:
- print("Military time should have exactly 4 digits.")
- print("0915, 2115, 0003, 1203\n")
- raise ValueError
- if not at.isdigit():
- print("There's a non-digit in either the hours or minutes or both.\n")
- raise ValueError
- at = at[:2] + ':' + at[2:]
- hour, minute = at.split(':')
- if not at.replace(':', '').isdigit():
- print("You've put a non-digit in either the hours or minutes or both.\n")
- raise ValueError
- if len(hour) != 2 or len(minute) != 2:
- print("Both hours and minutes should have exactly 2 digits.")
- print("09:15, 21:15, 00:03, 12:03\n")
- raise ValueError
- if int(hour) > 23:
- print("Hours must be 23 or less.\n")
- raise ValueError
- if int(minute) > 59:
- print("Minutes must be 59 or less.\n")
- raise ValueError
- print("\nIs", at, "the time you want? Hit enter for 'yes'; enter any character for 'no'.")
- ans = input("\nanswer?> ")
- if ans == '':
- return at
- else:
- raise ValueError
- except ValueError:
- # recursion
- return get_alarm_time()
- def get_alarm_time_from_user():
- 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
- """)
- return get_alarm_time()
- 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():
- 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()
- # 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