Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Recall that time in military format is based on a 24 hour clock. For example, 2:30pm is 1430 in military format, 2:30am is 0230,
Recall that time in military format is based on a 24 hour clock. For example, 2:30pm is 1430 in military format, 2:30am is 0230, 12:00am (midnight) is 0000 and 12:00pm (noon) is 1200. Note that the military time is always represented with four digits. In this problem, you are asked to write two functions to convert between the two formats. They are described below: (10 points) Write a function called ampm_to_military with a single parameter ampm_time, which is a string representing the time in AM/PM format. The function returns a string, which is the corresponding time in military format. The string ampm.time must have the following format to be valid: the hour (one or two characters), followed by a colon, followed by the minute (exactly two characters), followed by the letters "am" or "pm" (both upper and lower case are allowed). Thus, the length of the string ampm time should be either 6 or 7. Furthermore, the hour and minute should correspond to legitimate values. Your function should first check for the validity of ampa.time. If invalid, print an appropriate message and return the empty string. If it is valid, return the corresponding military time as a string of four characters. (10 points) Write a function called military.to.ampm with a single parameter mil_time, which is a string representing the time in military format. The function returns a string, which is the corresponding time in AM/PM format. The string mil.time must have the following format to be valid: it must be exactly four characters long, with the hour as the first two characters and the minute as the next two characters. In addition, the hour and minute should correspond to legitimate values. As in the above function, your function should first check for the validity of mil.time. If invalid, print an appropriate message and return the empty string. If it is valid, return the corresponding AM/PM time as a string of 6 or 7 characters. You will find the following Python string methods/functions useful here: The string method isdigit() returns True if all characters in the string are digits and there is at least one character, and False otherwise. For example, if astr="0345", antr.indigit() returns True. But if astr "aB9", astr.indigit() returns False. The function int (antr) returns the integer corresponding to the string antr. If astr has non-digit characters, you will get an error message. The function str (N) returns the string corresponding to the integer N. Here are some sample runs. >>> from problemi import. >>> military_to_ampa("1542") 3:42pm >>> ampa to military("9:06am") 0905 Here are some sample runs. >>> from problemi import >>> military_to_ampm("1542") 3:42pm >>> ampa_to_military (*9:05am") 0905 >>> ampm to military ("9:05pm") 2106 >>> ampm_to_military("9: 5pm") 9: 5pm in an invalid AM/PM timo! >>> military_to_ampm("3623") 3623 is an invalid military time! >>> military_to_ampm ("0005") 12:05am >>> nampm to military("11:65pm") 11:65pm is an invalid AM/PM timo! >>> ampa_to_military ("II:35pm) II:35pm is an invalid AM/PM time!
Step by Step Solution
★★★★★
3.34 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
Python program to create and test the functions ampmtomilitary and militarytoampm which converts the time from AMPM format to military and viceversa Python version 36 def ampmtomilitaryampmtime Functi...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started