Objective

practicing with implementing functions, string methods, loops, and elif statements

Description

In this assignment, you will write a Python program clock.py that emulates a digital clock and a countdown timer that can calculate and display time. At the beginning, the program prints a menu and asks the user to choose an operation such as setup time, setup alarm, show clock, run timer, or quit. Here are the snippets of the program output. Note: To get the full credit, please make sure that your program output matches the snippets exactly.

Digital Clock
Please enter one of the following choices [1-5]:
1. Setup Time
2. Setup Alarm
3. Show Clock
4. Run Timer
5. Quit

If the user did not choose correctly, it warns about wrong input and reprints the previous prompt:

Wrong choice!
Please enter one of the following choices [1-5]:
1. Setup Time
2. Setup Alarm
3. Show Clock
4. Run Timer
5. Quit

If the user chose 1 (setup time), then the program prints:

Please enter clock time [H:M:S]:

If the user does not enter time correctly, it reprints the message:

Please enter clock time [H:M:S]:

If the user chose 2 (setup alarm), then the program outputs:

Please enter alarm time [H:M:S]:

If the user does not enter time correctly, it reprints the message:

Please enter alarm time [H:M:S]:

If the user chose 3 (show clock), then the program outputs the clock time until it reaches the alarm time and prints the message "Beep! Beep! Beep!". If the clock time is not set previously (option 1 in the menu) then the default clock time is your computer local time. If the alarm time is not set, the default alarm time is 00:00:00.


_ _ _ _ _
_| | * | || | * |_ |
|_ | * |_||_| * _| |
_ _ _ _ _
_| | * | || | * |_ |_|
|_ | * |_||_| * _||_|
_ _ _ _ _
_| | * | || | * |_ |_|
|_ | * |_||_| * _| _|
_ _ _ _
_| | * | | | * | || |
|_ | * |_| | * |_||_|
Beep! Beep! Beep!

After that the program reprints the menu.

If the user chose 4 (run timer), then the program prints the following message:

Please enter countdown time [H:M:S]:

If the user did not enter time correctly, the program reprints the message:

Please enter countdown time [H:M:S]:

Otherwise, the program counts down time and displays it until it reaches zero time and prints the message "Beept! Beept! Beept!":


_ _ _ _ _ _
| || | * | || | * | | _|
|_||_| * |_||_| * |_||_
_ _ _ _ _
| || | * | || | * | | |
|_||_| * |_||_| * |_| |
_ _ _ _ _ _
| || | * | || | * | || |
|_||_| * |_||_| * |_||_|
Beep! Beep! Beep!

After that the program reprints the menu.

If the user chose 5 (quit), then the program prints the following message and terminates:

Goodbye!

Programming Approaches

You can use the template file clock.py posted in Files/Scripts/PA3. Remember your program should start with the header (a comment block) and import statements:

# assignment: programming assignment 3
# author: (type your full name here)
# date: (type the date you finished working on the program)
# file: clock.py is a program that (type the description of the program)
# input: (type the input description)
# output: (type the output description)

import time, datetime
# needed to create time objects and to use the time .sleep() function

import winsound
# optional, works only for Windows OS, not Mac OS from os import system, name
# optional, needed to clear the screen, works only in the terminal, not in the python shell, should not be used in the final version for testing/grading

In this assignment, you should create at least the following six functions: set_time(), set_alarm(),
run_timer(), run_clock(), convert_secs_time(secs), and
convert_time_secs(t). Their definition headings should
be written exactly as the following function definition
headings:

def set_time():
# set the clock time relatively to the local comput er time
# calculates the difference in secs between the loc al time and the setup time
# the difference should be declared as a global var iable

def set_alarm():
# set the alarm time
# the alarm time should be set as a global variable def run_timer():
# set the countdown time and run the countdown time r, may use sound effects
def run_clock():

# show the clock time, ring alarm, may use sound effects
# the clock time can be calculated as the difference between the computer local time
# and the global variable that holds the difference between the setup time and the clock time
def convert_time_secs(t):

# t is a time data type object that is created as t = datetime.time(hours, mins, secs)
# where hours, mins, secs are integers in the range s 0-23, 0-59, 0-59
# return secs

def convert_secs_time(secs):
# secs is the total time in secs
# return a tuple of three integers
# return (hours, mins, secs)

def clear():
# optional, clears the terminal screen, should not be used in the final version for grading
# for windows (os.name is nt)
if name == 'nt':
_ = system('cls')
# for mac and linux(os.name is posix)
else:
_ = system('clear')

def display_time(clock):
# should be used for displaying time (clock time or countdown time)
# clock is a time data type object
row1, row2, row3 = '','',''
count = 0
for t in [clock.hour, clock.minute, clock.secon
d]:
if t < 10:
r1,r2,r3 = get_sign(0)
else:
r1,r2,r3 = get_sign(t//10) # get first
digit
row1 += r1
row2 += r2
row3 += r3
r1,r2,r3 = get_sign(t%10) # get second
digit
row1 += r1
row2 += r2
row3 += r3
if count < 2:
count += 1
r1,r2,r3 = get_sign(-1) # get delimi
ter
row1 += r1
row2 += r2
row3 += r3
print(row1)
print(row2)
print(row3)

def get_sign(sign):
# supplementary function used in display_time()that makes digits using ascii graphics
if sign == 0:
row1 = ' _ '
row2 = '| |'
row3 = '|_|'
elif sign == 1:
row1 = ' '
row2 = ' | '
row3 = ' | '
elif sign == 2:
row1 = ' _ '
row2 = ' _|'
row3 = '|_ '
elif sign == 3:
row1 = ' _ '
row2 = ' _|'
row3 = ' _|'
elif sign == 4:
row1 = ' '
row2 = '|_|'
row3 = ' |'
elif sign == 5:
row1 = ' _ '
row2 = '|_ '
row3 = ' _|'
elif sign == 6:
row1 = ' _ '
row2 = '|_ '
row3 = '|_|'
elif sign == 7:
row1 = ' _ '
row2 = ' |'
row3 = ' |'
elif sign == 8:
row1 = ' _ '
row2 = '|_|'
row3 = '|_|'
elif sign == 9:
row1 = ' _ '
row2 = '|_|'
row3 = ' _|'
else:
row1 = ' '
row2 = ' * '
row3 = ' * '
return(row1, row2, row3)

# main program
if __name__ == "__main__":
print('Digital Clock')
# print menu

Your program should calculate time using the local time that can be obtained in the following way:

hours = datetime.datetime.now().hour # get hours
mins = datetime.datetime.now().minute # get mins
secs = datetime.datetime.now().second # get secs
localtime = datetime.time(hours, mins, secs) # create a time object

You can use time.sleep() in the loops where you decrement or increment time. For example:

while True:
display_time(t)
... (update time)
time.sleep(1) # set the sleeping interval to 1 second

Optional Features. To make sounds on Windows computers, you can use the following code: import winsound winsound.PlaySound("Media/Ring07.wav", winsound.SND_FILENAME)

You can choose another audio file instead of "Media/Ring07.wav".

For Mac and Linux you can try to print the bell sign: print('\a'). It may not work on some systems. You can install and use additional libraries, if you want. You can check this webpage for guidelines: https://pythonin1minute.com/how-to-beep-in-python/

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.