pyzuki

Alarm Clock v2.0

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