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
Create an application named TestClassifiedAd that instantiates and displays at least two ClassifiedAd objects. A ClassifiedAd ha
arlik [135]

Answer:

Following is given the code with all necessary descriptions as comments in it. The output is also attached under the code. I hope it will help you!

Explanation:

5 0
3 years ago
A friend of yours started her own dog walking business. At first she only had a couple customers, and she used Word to create al
GaryK [48]
Hire more people to help type her documents, or make Batch invoices
4 0
3 years ago
Complete the paragraph describing characteristics of an audio mixer
Marizza181 [45]

Answer:

XLR inputs and preamps with adjustable trim, EQ, aux buses, pan pots, and faders are the requisite features of every audio mixer, while higher-end large-format consoles may have an additional dynamics section as well.

Explanation:

3 0
4 years ago
Read 2 more answers
In which state of matter is there no particle motion?
kiruha [24]
Solid is your answer for the day
6 0
3 years ago
Dang was accepted to a biology program with a rigorous schedule and a high tuition, but good professors. What would be a benefit
Amiraneli [1.4K]
The benefit would be C. If you look at it and put yourself in their situation, it'd most likely be C that you think is most beneficial.
6 0
4 years ago
Read 2 more answers
Other questions:
  • After creating an organizational strategy based on porter's models, a company can?
    14·2 answers
  • When you choose a(n) ___________ installation, you only install selected features, rather than all of the features associated wi
    13·1 answer
  • ‘‘Anyone in the developed world can publish anything anytime, and the instant it is published, it is globally available and read
    7·1 answer
  • True/False: Software engineering is a field that encompasses designing, writing, testing, debugging, documenting, modifying, and
    6·1 answer
  • What two statements about IPv6 addresses are true? This task contains the radio buttons and checkboxes for options. The shortcut
    7·1 answer
  • 100 tickets are sold in a raffle with one prize. John has 8 tickets, Jamie has 10 tickets, find the probability that John wins
    11·2 answers
  • ____ allows you to add formatting such as shapes and colors to text
    8·1 answer
  • Write a python program to calculate the average of two numbers​
    8·1 answer
  • What happens in the development phase of the software development life cycle?
    10·1 answer
  • How to create drop down list in excel with multiple selections.
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!