pyzuki

Alarm Clock 4.0

Oct 2nd, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.21 KB | None | 0 0
  1. # Alarm Clock 4.0  
  2.  
  3.  
  4. from time import ctime, clock, sleep
  5. import winsound
  6. import sys
  7.  
  8. def tadas_time(n):
  9.     return round((int(n)-1) * 1.79 + 1, 1)
  10.  
  11. def get_num_tadas_from_user():
  12.     try:
  13.         print("Enter the number of taDAHs desired; Entering nothing will set the number to 1")
  14.         n = input("\nHow many tadas?> ")
  15.         if not n:
  16.             print("The number of taDAHs was set to 1")
  17.             return 1
  18.         if int(n) < 1:
  19.             print("The number of taDAHs must be at least 1, or the alarm won't sound")
  20.             raise ValueError
  21.         if int(n) > 3:
  22.             print("Are you sure you want", n, "taDAHs? That will take", tadas_time(n), "seconds")
  23.             print("Hit enter for 'yes'; enter any character if you want to change the number")
  24.             ans = input("\nanswer?> ")
  25.             if ans == '':
  26.                 print("The number of taDAHs was set to", n)
  27.                 return int(n)
  28.             else:
  29.                 raise ValueError
  30.         print("The number of taDAHs was set to", n)
  31.         return int(n)
  32.     except ValueError:
  33.         # recursion
  34.         return get_num_tadas_from_user()
  35.        
  36. def get_message_from_user():
  37.     """
  38.    The message is printed when the alarm rings.
  39.    The idea is that the message will tell the user the reason
  40.    for the alarm. Examples are, "Feed the cat", "Go to bed", "Write to Nick".
  41.    """
  42.     print("""
  43.    Enter a message about what the alarm ringing means,
  44.    in case you forget, or have several alarms set.
  45.    Enter nothing if no message.
  46.    """)
  47.     message = input("\nMessage?> ")
  48.     if message == '':
  49.         print("no message entered")
  50.         return None
  51.     else:
  52.         return message
  53.    
  54. def find_alpha(at):
  55.     for char in at:
  56.         if char.isalpha():
  57.             return char
  58.     return None
  59.  
  60. def get_alarm_time():
  61.     alarm_time = input("alarm time?> ")
  62.     at = alarm_time
  63.     try:
  64.         # User hits Enter before typing his alarm time
  65.         if not at:
  66.             raise ValueError
  67.         # User thinks that hours and minutes should be separated by a space
  68.         if ' ' in at:
  69.             print("Do not use a space anywhere in alarm time.")
  70.             print("Use a colon to separate hours and minutes: 10:45, 03:07")
  71.             print("Or use military time: 0915, 2115, 0003, 1203\n")
  72.             raise ValueError
  73.         # User somehow thinks alarm time should or could be negative
  74.         if at[0] == '-':
  75.             print("alarm time cannot be negative.\n")
  76.             raise ValueError
  77.         # User enters +3:00. Possibly thinking he wants the alarm to ring 3 hours from now?\n"
  78.         if '+' in at:
  79.             print("Do not use '+' anywhere in alarm time.\n")
  80.             raise ValueError
  81.         #User thinks he should use 'l' for '1'.
  82.         if 'l' in at:
  83.             print("You've used an 'l' (lower case 'L') in your alarm time instead of the digit '1' (one).\n")
  84.             raise ValueError
  85.         # User enters 1o45 or ooo3
  86.         if 'o' in at:
  87.             print("You've used the letter 'o' in your alarm time instead of the digit '0' (zero).\n")
  88.             raise ValueError
  89.         # User enters 1O45 or OOO3
  90.         if 'O' in at:
  91.             print("You've used the letter 'O' in your alarm time instead of the digit '0' (zero).\n")
  92.             raise ValueError
  93.         # User uses an alphabetic letter other than the above o, O, or l
  94.         char = find_alpha(at)
  95.         if char:
  96.             print("Do not use an alphabetic letter such as '", char, "' in alarm time.\n", sep='')
  97.             raise ValueError
  98.        
  99.         if len(at) == 5:
  100.             # User enters 12345
  101.             if at.isdigit():
  102.                 print("Military time should have exactly 4 digits, not 5.")
  103.                 print("0915, 2115, 0003, 1203\n")
  104.                 raise ValueError
  105.             # User enters 10;45
  106.             if at[2] == ';' and at.replace(';', '').isdigit():
  107.                 print("You've used a semicolon between hours and minutes. Use a colon: 10:45, 03:07.\n")
  108.                 raise ValueError
  109.             # User enters 10,45 or 10.45 or 10/45, or 10x45, etc.
  110.             if at[2] != ':' and at.replace(at[2], '').isdigit():
  111.                 print("Use a colon between hours and minutes: 10:45, 03:07\n")
  112.                 raise ValueError
  113.            
  114.         if ':' not in at:
  115.             # User enters 123 or 123456
  116.             if len(at) != 4:
  117.                 print("Military time should have exactly 4 digits.")
  118.                 print("0915, 2115, 0003, 1203\n")
  119.                 raise ValueError
  120.            
  121.             if not at.isdigit():
  122.                 print("There's a non-digit in either the hours or minutes or both.\n")
  123.                 raise ValueError
  124.            
  125.             at = at[:2] + ':' + at[2:]  
  126.            
  127.         hour, minute = at.split(':')
  128.        
  129.         if not at.replace(':', '').isdigit():
  130.             print("You've put a non-digit in either the hours or minutes or both.\n")
  131.             raise ValueError
  132.        
  133.         if len(hour) != 2 or len(minute) != 2:
  134.             print("Both hours and minutes should have exactly 2 digits.")
  135.             print("09:15, 21:15, 00:03, 12:03\n")
  136.             raise ValueError
  137.        
  138.         if int(hour) > 23:
  139.             print("Hours must be 23 or less.\n")
  140.             raise ValueError
  141.        
  142.         if int(minute) > 59:
  143.             print("Minutes must be 59 or less.\n")
  144.             raise ValueError
  145.        
  146.         print("\nIs", at, "the time you want? Hit enter for 'yes'; enter any character for 'no'.")
  147.         ans = input("\nanswer?> ")
  148.         if ans == '':
  149.             return at
  150.         else:
  151.             raise ValueError
  152.        
  153.     except ValueError:
  154.         # recursion
  155.         return get_alarm_time()
  156.    
  157. def get_alarm_time_from_user():
  158.     print("""
  159.    Enter an alarm time in 24-hour time.
  160.    Both hours and minutes should be in 2 digits, separated by a colon.
  161.    13:00, 22:45, 02:30, 00:05
  162.    Military time will be converted to 24-hour time.
  163.    1300 -> 13:00, 0005 -> 00:05
  164.    """)
  165.     return get_alarm_time()
  166.      
  167. def tada_or_tadas(num_tadas):
  168.     """
  169.    Make "tada" plural when appropriate.
  170.    
  171.    "tada" is one of the sounds that come with Windows: taDAH.
  172.    """
  173.     if num_tadas == 1:
  174.         return "taDAH"
  175.     else:
  176.         return "taDAHs"
  177.    
  178. def time2seconds(alarm_time):
  179.     """time_string format = hh:mm, returns seconds as int."""
  180.     hour, minute = alarm_time.split(':')
  181.     return int(hour)*3600 + int(minute)*60
  182.    
  183. def today_or_tomorrow(alarm_time):
  184.     now = ctime()[11:16]
  185.     if time2seconds(now) > time2seconds(alarm_time):
  186.         return "tomorrow"
  187.     else:
  188.         return "today"
  189.    
  190. def print_summary(alarm_time, today_tomorrow, num_tadas, message):
  191.     print("\nThe alarm is set to ring at", alarm_time, today_tomorrow)
  192.     if message:
  193.         print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
  194.     else:
  195.         print("with", num_tadas, tada_or_tadas(num_tadas), "and no message.")
  196.        
  197. def display_current_time():
  198.     """
  199.    Prints the current time as 24-hour time -- but no seconds.
  200.    13:07, 05:00, 23:59, 00:02, etc.
  201.    Each printing overwrites the previous one.
  202.    """
  203.     sys.stdout.flush()
  204.     print("The time now is", ctime()[11:16], "\r", end='')
  205.      
  206. def main():
  207.     print("\n" * 9)
  208.    
  209.     # Get input from user.
  210.     num_tadas = get_num_tadas_from_user()
  211.     message = get_message_from_user()
  212.     alarm_time = get_alarm_time_from_user()
  213.    
  214.     # Print summary of alarm info.
  215.     today_tomorrow = today_or_tomorrow(alarm_time)
  216.     print_summary(alarm_time, today_tomorrow, num_tadas, message)
  217.    
  218.     # Main loop (wait until alarm time).
  219.     print()
  220.     while alarm_time != ctime()[11:16]:
  221.         sleep(0.1)
  222.         display_current_time()
  223.        
  224.     # Sound alarm
  225.     for tada in range(0, num_tadas):
  226.         winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
  227.        
  228.     # Display alarm message and close.
  229.     if message:
  230.         print("\nmessage:", message, "\n")
  231.     print()
  232.     print("Hit Enter to close Alarm Clock.")
  233.     input()
  234.    
  235.    
  236. if __name__ == '__main__':
  237.     main()
  238.    
  239.  
  240.  
Advertisement
Add Comment
Please, Sign In to add comment