Programming Assignment 1 (Say Military Time)
Military time is a notation for specifying time of day using the form hhmm, where hh denotes the number of full hours that have passed since midnight and mm denotes the number of full minutes that have passed since the last full hour. For example, 1200 is noon; 2359 is one minute before midnight; and 0100 is one hour after midnight.
Write a Python program (SayMilitaryTime.py) that takes as input one or two command line inputs: the first mandatory input being a number denoting a military time and an optional accent string (AUS, IND, or USA). The program should print the military time in words and play a sound recording of the military time in the specified accent. If the accent is not provided, play the sound recording in the USA accent.
To say the military time, use the following conventions:
- Say the number of hours hh; if it is between 1 and 9, precede it with the word zero.
- Say the word hundred.
- Say the number of minutes mm (unless it is 00).
- Say the word hours.
Some Notes:
To read command line parameters, you can use the folowing code:
import sys military_time = sys.argv[1] accent = sys.argv[2]
- To convert text to sound, you should use Google's text to sound library, gtts,
which can be installed as follows on the terminal (pip3 for Mac and pip for Windows):
pip3 install gtts or pip install gtts
The gtts library can be used as follows to convert text to sound file:from gtts import gTTS audio = gTTS(text="twelve hundred thirty four hours", lang='en') audio.save("f.mp3")
Documentation for gtts is available at https://gtts.readthedocs.io/en/latest/module.html. - To play a sound file from within Python, you can use the preferredsoundplayer
library, which can be installed as follows in the terminal (pip3 for Mac and pip for Windows):
pip3 install preferredsoundplayer or pip install preferredsoundplayer
To play a sound file, you can use the following code:from preferredsoundplayer import soundplay soundplay("./f.mp3")
Documentation for preferredsoundplayer is available at https://pypi.org/project/preferredsoundplayer/. - The military time command line input may be provided in 3 or 4 digits.
- The accent command line input may be provided in upper (AUS), lower case (aus), or mixed case (Aus).
- Inputs should be checked for validity and error should be reported (if there are more than one errors,
you should display just one of the errors). The following shows several error possibilities:
$ python3 SayMilitaryTime.py 1234 aus ind You need to provide command line inputs. Usage: python3 SayMilitaryTime.py 1859 USA $ python3 SayMilitaryTime.py 1245 Mex Invalid 2nd argument. Must be one of IND, AUS, USA $ python3 SayMilitaryTime.py 1064 Aus Invalid 1st command line argument. Minutes cannot be more than 59. $ python3 SayMilitaryTime.py 2959 Aus Invalid 1st command line argument. Hours cannot be more than 23. $ python3 SayMilitaryTime.py 220a Aus Invalid 1st command line argument. Should contain only digits.
- To catch the "Should contain only digits" error, you can use a try-except statement
in Python:
try: mtime = int(sys.argv[1]) except: print("Invalid 1st command line argument. Should contain only digits.") sys.exit()
- Here are some functions that can be developed to solve the problem:
# returns a string for the word(s) corresponding to the input parameter, minutes. def convert_minutes(minutes): pass # returns a string for the word(s) corresponding to the input parameter, hours. def convert_hours(hours): pass # The main function that reads command line inputs, checks for errors, # determines hours and minutes for the input military time, calls the above # functions in order, prints military time in words, converts time in words # to a sound file, and finally plays the sound file. def main(): pass