pyzuki

alarm_clock64a

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