pyzuki

Alarm Clock 4.1

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