Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # alarm_clock62a.py need to incorporate Nick's refactorization https://gist.github.com/1246851
- # in alarm_clock61a.py changed get_num_tadas_from_user () and get_message_from_user()
- # completed through print_summary()
- from time import ctime, clock, sleep
- import winsound
- import sys
- def get_num_tadas_from_user():
- try:
- n = input("\nHow many tadas?> ")
- if not n:
- return 1
- else:
- return int(n)
- except ValueError:
- return get_num_tadas_from_user()
- def get_message_from_user():
- """
- The message is printed when the alarm rings.
- The idea is that the message will tell the user the reason
- for the alarm. Examples are, "Feed the cat", "Go to bed", "Write to Nick".
- """
- print("""
- Enter a message about what the alarm ringing means,
- in case you forget, or have several alarms set.
- Enter nothing if no message.
- """)
- message = input("\nMessage?> ")
- if message == '':
- print("no message entered")
- return None
- else:
- return message
- def convert_from_military_time(time):
- """Converts military time to hh:mm, raises ValueError if invalid."""
- if len(time) == 4 and time.isdigit():
- return time[:2] + ':' + time[2:]
- else:
- raise ValueError
- def get_alarm_time_from_user():
- print("""
- Enter an alarm time in 24-hour time.
- Both hours and minutes should be in 2 digits, separated by a colon.
- 13:00, 22:45, 02:30, 00:05
- Military time will be converted to 24-hour time
- 1300 -> 13:00, 0005 -> 00:05
- """)
- alarm_time = input("alarm time?> ")
- if ':' not in alarm_time:
- alarm_time = convert_from_military_time(alarm_time)
- hour, minute = alarm_time.split(':')
- try:
- if ':' not in alarm_time:
- alarm_time = convert_from_military_time(alarm_time)
- if not (alarm_time.replace(':', '').isdigit()):
- raise ValueError
- if len(hour) != 2 or len(minute) != 2:
- raise ValueError
- if not 0 <= int(hour) < 24 or 0 <= int(minute) < 60:
- raise ValueError
- print("\nIs", alarm_time, "the time you want? Hit enter for 'yes'; enter any character for 'no'")
- if input("\nanswer?> "):
- raise ValueError
- return alarm_time
- except ValueError:
- return get_alarm_time_from_user
- def today_or_tomorrow(alarm_time):
- now = ctime()[11:16]
- if time2seconds(now) > time2seconds(alarm_time):
- return "tomorrow"
- else:
- return "today"
- def tada_or_tadas(num_tadas):
- """
- Make "tada" plural when appropriate.
- "tada" is one of the sounds that come with Windows: taDAH.
- """
- if num_tadas == 1:
- return "taDAH"
- else:
- return "taDAHs"
- def time2seconds(hour, minute):
- """Given hour and minute as strings, return seconds as int."""
- return int(hour)*3600 + int(minute)*60
- def display_current_time():
- """
- Prints the current time as 24-hour time -- but no seconds.
- 13:07, 05:00, 23:59, 00:02, etc.
- Each printing overwrites the previous one.
- """
- sys.stdout.flush()
- print("The time now is", ctime()[11:16], "\r", end='')
- def print_summary(alarm_time, today_tomorrow, num_tadas, message):
- print("\nThe alarm is set to ring at", alarm_time, today_tomorrow)
- if message:
- print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
- else:
- print("with", num_tadas, tada_or_tadas(num_tadas), "and no message.")
- def ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas):
- print()
- while alarm_time != ctime()[11:16]:
- sleep(0.1)
- display_current_time()
- for tada in range(0, num_tadas):
- winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
- def main():
- print("\n" * 9)
- num_tadas = get_num_tadas_from_user()
- message = get_message_from_user()
- alarm_time = get_alarm_time_from_user()
- today_tomorrow = today_or_tomorrow(alarm_time)
- print_summary(alarm_time, today_tomorrow, num_tadas, message)
- ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas)
- if message:
- print("\nmessage:", message, "\n")
- print()
- print("Hit Enter to close Alarm Clock")
- input()
- if __name__ == '__main__':
- main()
- """
- ERROR:
- How many tadas?> 2
- Enter a message about what the alarm ringing means,
- in case you forget, or have several alarms set.
- Enter nothing if no message.
- Message?> Feed Toby
- Enter an alarm time in 24-hour time.
- Both hours and minutes should be in 2 digits, separated by a colon.
- 13:00, 22:45, 02:30, 00:05
- Military time will be converted to 24-hour time
- 1300 -> 13:00, 0005 -> 00:05
- alarm time?> 1500
- Traceback (most recent call last):
- File "C:\P32Working\alarm_clock62a.py", line 146, in <module>
- main()
- File "C:\P32Working\alarm_clock62a.py", line 135, in main
- today_tomorrow = today_or_tomorrow(alarm_time)
- File "C:\P32Working\alarm_clock62a.py", line 86, in today_or_tomorrow
- if time2seconds(now) > time2seconds(alarm_time):
- TypeError: time2seconds() takes exactly 2 arguments (1 given)
- Process terminated with an exit code of 1
- """
Advertisement
Add Comment
Please, Sign In to add comment