pyzuki

Alarm Clock 3.0

Sep 29th, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.72 KB | None | 0 0
  1. Alarm Clock 3.0
  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.         return get_num_tadas_from_user()
  33.        
  34. def get_message_from_user():
  35.     """
  36.    The message is printed when the alarm rings.
  37.    The idea is that the message will tell the user the reason
  38.    for the alarm. Examples are, "Feed the cat", "Go to bed", "Write to Nick".
  39.    """
  40.     print("""
  41.    Enter a message about what the alarm ringing means,
  42.    in case you forget, or have several alarms set.
  43.    Enter nothing if no message.
  44.    """)
  45.     message = input("\nMessage?> ")
  46.     if message == '':
  47.         print("no message entered")
  48.         return None
  49.     else:
  50.         return message
  51.      
  52. def get_alarm_time_from_user():
  53.     print("""
  54.    Enter an alarm time in 24-hour time.
  55.    Both hours and minutes should be in 2 digits, separated by a colon.
  56.    13:00, 22:45, 02:30, 00:05
  57.    Military time will be converted to 24-hour time
  58.    1300 -> 13:00, 0005 -> 00:05
  59.    """)
  60.    
  61.     while True:
  62.         alarm_time = input("alarm time?> ")
  63.        
  64.         # The user uses something other than a colon (say a semicolon)
  65.         # to separate hour and minute
  66.         if len(alarm_time) == 5 and alarm_time[2] != ':':
  67.             print("A colon should come between hours and minutes: 10:45.\n")
  68.             continue
  69.        
  70.         if ':' not in alarm_time:
  71.             if len(alarm_time) != 4:
  72.                 print("Military time should have exactly 4 digits.\n")
  73.                 continue
  74.             if not alarm_time.isdigit():
  75.                 print("There's a non-digit in either the hours or minutes or both.\n")
  76.                 continue
  77.             alarm_time = alarm_time[:2] + ':' + alarm_time[2:]  
  78.            
  79.         if not (alarm_time.replace(':', '').isdigit()):
  80.             print("You've put a non-digit in either the hours or minutes or both.\n")
  81.             continue
  82.        
  83.         hour, minute = alarm_time.split(':')
  84.         if len(hour) != 2 or len(minute) != 2:
  85.             print("Both hours and minutes should have exactly 2 digits.\n")
  86.             continue
  87.        
  88.         if not 0 <= int(hour) < 24:
  89.             print("Hours should be >= 0 and less than 24.\n")
  90.             continue
  91.        
  92.         if not 0 <= int(minute) < 60:
  93.             print("Minutes should be >= 0 and less than 60.\n")
  94.             continue
  95.    
  96.         print("\nIs", alarm_time, "the time you want? Hit enter for 'yes'; enter any character for 'no'.")
  97.         ans = input("\nanswer?> ")
  98.         if ans == '':
  99.             return alarm_time
  100.         else:
  101.             continue
  102.        
  103.        
  104.    
  105. def tada_or_tadas(num_tadas):
  106.     """
  107.    Make "tada" plural when appropriate.
  108.    
  109.    "tada" is one of the sounds that come with Windows: taDAH.
  110.    """
  111.     if num_tadas == 1:
  112.         return "taDAH"
  113.     else:
  114.         return "taDAHs"
  115.    
  116. def time2seconds(alarm_time):
  117.     """time_string format = hh:mm, returns seconds as int."""
  118.     hour, minute = alarm_time.split(':')
  119.     return int(hour)*3600 + int(minute)*60
  120.    
  121. def today_or_tomorrow(alarm_time):
  122.     now = ctime()[11:16]
  123.     if time2seconds(now) > time2seconds(alarm_time):
  124.         return "tomorrow"
  125.     else:
  126.         return "today"
  127.    
  128. def print_summary(alarm_time, today_tomorrow, num_tadas, message):
  129.     print("\nThe alarm is set to ring at", alarm_time, today_tomorrow)
  130.     if message:
  131.         print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
  132.     else:
  133.         print("with", num_tadas, tada_or_tadas(num_tadas), "and no message.")
  134.        
  135. def display_current_time():
  136.     """
  137.    Prints the current time as 24-hour time -- but no seconds.
  138.    13:07, 05:00, 23:59, 00:02, etc.
  139.    Each printing overwrites the previous one.
  140.    """
  141.     sys.stdout.flush()
  142.     print("The time now is", ctime()[11:16], "\r", end='')
  143.      
  144. def main():
  145.     print("\n" * 9)
  146.    
  147.     # Get input from user.
  148.     num_tadas = get_num_tadas_from_user()
  149.     message = get_message_from_user()
  150.     alarm_time = get_alarm_time_from_user()
  151.    
  152.     # Print summary of alarm info.
  153.     today_tomorrow = today_or_tomorrow(alarm_time)
  154.     print_summary(alarm_time, today_tomorrow, num_tadas, message)
  155.    
  156.     # Main loop (wait until alarm time).
  157.     print()
  158.     while alarm_time != ctime()[11:16]:
  159.         sleep(0.1)
  160.         display_current_time()
  161.        
  162.     # Sound alarm
  163.     for tada in range(0, num_tadas):
  164.         winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
  165.        
  166.     # Display alarm message and close.
  167.     if message:
  168.         print("\nmessage:", message, "\n")
  169.     print()
  170.     print("Hit Enter to close Alarm Clock.")
  171.     input()
  172.    
  173.    
  174. if __name__ == '__main__':
  175.     main()
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment