1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nataliya [291]
3 years ago
6

Write a program that inputs a time from the console. The time should be in the format "HH:MM AM" or "HH:MM PM". Hours may be one

or two digits, for example, "1:10 AM" or "11:30 PM". Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four-digit military time based on a 24-hour clock. For example, "1:10 AM" would output "0110 hours", "11:30 PM" would output "2330 hours", and "12:15 AM" would output "0015 hours". The function should return a string to main for writing it out to the console. (Do not output the value in the function.) Sample runs of the program:
Computers and Technology
1 answer:
monitta3 years ago
5 0

Answer:

  1. def processTime(timeStr):
  2.    timeData = timeStr.split(" ")
  3.    timeComp = timeData[0].split(":")
  4.    h = int(timeComp[0])
  5.    m = int(timeComp[1])
  6.    
  7.    output = ""
  8.    if(timeData[1] == "AM"):
  9.        if(h < 10):
  10.            output += "0" + str(h) + str(m) + " hours"
  11.        elif(h == 12):
  12.            output += "00" + str(m) + " hours"
  13.        else:
  14.            output += str(h) + str(m) + " hours"
  15.    else:
  16.        h = h + 12  
  17.        output += str(h) + str(m) + " hours"
  18.    return output  
  19. print(processTime("11:30 PM"))

Explanation:

The solution code is written in Python 3.

Firstly, create a function processTime that takes one input timeStr with format "HH:MM AM" or "HH:MM PM".

In the function, use split method and single space " " as separator to divide the input time string into "HH:MM" and "AM" list items (Line 2)

Use split again to divide first list item ("HH:MM") using the ":" as separator into two list items, "HH" and "MM" (Line 3). Set the HH and MM to variable h and m, respectively (Line 4-5)

Next create a output string variable (Line 7)

Create an if condition to check if the second item of timeData list is "AM". If so, check again if the h is smaller than 10, if so, produce the output string by preceding it with a zero and followed with h, m and " hours" (Line 9 - 11). If the h is 12, precede the output string with "00" and followed with h, m and " hours" (Line 12 - 13). Otherwise generate the output string by concatenating the h, m and string " hours" (Line 14 -15)

If the second item of timeData is "PM", add 12 to h and then generate the output string by concatenating the h, m and string " hours" (Line 17 -18)

Test the function (Line 22) and we shall get the sample output like 2330 hours

You might be interested in
8.
mario62 [17]

Answer:

<em>Lillipop</em>

Explanation:

If you have your hands correctly placed on the keyboard, the only hand needed to type the word, "lillipop", is the left hand!

<em>Hope I was of Assistance</em><u><em> #SpreadTheLove <3</em></u>

5 0
3 years ago
What are the answers to 14 and 15
RoseWind [281]
The answer to 14 is c and the answer to 16 is a
6 0
4 years ago
E-mails that are mailed directly to a consumer without their knowledge or permission are called
gizmo_the_mogwai [7]
E-mails that are mailed directly to a consumer without knowledge or permission are called spam
6 0
3 years ago
Read 2 more answers
1.
slamgirl [31]
1. I believe it this, they way the question is phrased it seems like a personal opinion. But if we're being logical about this, no one likes background noise. So,yes, it's important. 
2. Yes, when you record something you don't want additional sound that has nothing to do with what you're recording. 
3.Yes, your main task is to be focused on what you're doing on the computer. You don't want to be distracted by unnecessary distractions. 
8 0
3 years ago
A ________ is a collection of computers connected to one another or to a common connection medium.
RoseWind [281]
Local area network <span>(LAN)</span>
3 0
3 years ago
Read 2 more answers
Other questions:
  • Alan wants to find an image of a car he can use in a presentation. What button should he click in the Images group to run a sear
    5·1 answer
  • Hw to gain more knowledge ​
    6·1 answer
  • How do I write a letter on my computer and print it?
    9·2 answers
  • ___ a Word object in an Excel worksheet to activate the Word features. A. Ctrl-click. B. Shift-click C. Double-click D. Click
    15·2 answers
  • Matt is a senior developer for Cyber Protect, a company that helps secure management information systems. Matt's new task is to
    8·1 answer
  • Where is voice data stored in Android?
    11·1 answer
  • A Windows user right-clicks on his desktop. What is he planning to do?
    10·1 answer
  • Name three output devices
    6·1 answer
  • Which scenario is best for an online discussion group?
    5·1 answer
  • PLEASE ANSWER CORRECTLY
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!