Question

In Python how do I : Generate a 12 hour clock (hh:mm am/pm) randomly and then...

In Python how do I :

  • Generate a 12 hour clock (hh:mm am/pm) randomly and then convert that clock to the equivalent 24 hour clock (hh:mm).
  • AND
  • Generate a 24 hour clock (hh:mm) randomly and then convert that clock to the equivalent 12 hour clock (hh:mm am/pm).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for converting random 12 hour clock to 24 hour clock:

I am giving 2 methods for it (i.e. two codes)

1)

-------------------------------------------------------------------------------------------------------------------------------------------------------------

import random
hh = random.randint(1,12)
mm = random.randint(0,60)
x = random.randint(0,1)

def convert24(str1):
  
# Checking if last two elements of time
# is AM and first two elements are 12
if str1[-2:] == "AM" and str1[:2] == "12":
return "00" + str1[2:-2]
  
# remove the AM   
elif str1[-2:] == "AM":
return str1[:-2]
  
# Checking if last two elements of time
# is PM and first two elements are 12
elif str1[-2:] == "PM" and str1[:2] == "12":
return str1[:-2]
  
else:
  
# add 12 to hours and remove PM
return str(int(str1[:2]) + 12) + str1[2:6]

if mm>=0 and mm<=9:
   m = "0" +str(mm)
else:
   m = str(mm)  

if hh>=0 and hh<=9:
   h = "0" +str(hh)
else:
   h = str(hh)  


if x == 1:
   z = "PM"
if x==0:
   z = "AM"
# Driver Code   
print(h+":"+ m+" "+z)
print(convert24(h+":"+ m+" "+z))

-------------------------------------------------------------------------------------------------------------------------------------------------------------

2)

------------------------------------------------------------------------------------------------------------------------------------------------------------

from datetime import datetime
import random
hh = random.randint(1,12)
mm = random.randint(0,60)
x = random.randint(0,1)

if mm>=0 and mm<=9:
   m = "0" +str(mm)
else:
   m = str(mm)  

if hh>=0 and hh<=9:
   h = "0" +str(hh)
else:
   h = str(hh)  
if x == 1:
   z = "PM"
if x==0:
   z = "AM"

d = datetime.strptime(h+":"+m+" "+z,"%I:%M %p" )
print(h+":"+m+" "+z)
print(d.strftime("%H:%M"))

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Now Code to convert 24 hour clock to 12 hour clock:

------------------------------------------------------------------------------------------------------------------------------------------------------

from datetime import datetime
import random
hh = random.randint(0,23)
mm = random.randint(0,60)

if mm>=0 and mm<=9:
   m = "0" +str(mm)
else:
   m = str(mm)  

if hh>=0 and hh<=9:
   h = "0" +str(hh)
else:
   h = str(hh)  


d = datetime.strptime(h+":"+m, "%H:%M")
print(h+":"+m)
print(d.strftime("%I:%M %p"))

------------------------------------------------------------------------------------------------------------------------------------------------------------

Outputs for both the codes:

12 hour to 24 hour:

Activities Terminal Tue Jul 2, 02:50 aakash@aakash-HP-Notebook: ~- File Edit View Search Terminal Help aakash@aakash- HP- Not

24 hour to 12 hour

Activities Terminal 7 4 95 %. Tue Jul 2,02:53 aakash@aakash-HP-Notebook: ~ File Edit View Search Terminal Help aakash@aakash-

Add a comment
Know the answer?
Add Answer to:
In Python how do I : Generate a 12 hour clock (hh:mm am/pm) randomly and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone solve it with C plz Write a program that inputs a time from the...

    Can someone solve it with C plz 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...

  • How do I generate a banded matrix in Python programming language? I need the function of...

    How do I generate a banded matrix in Python programming language? I need the function of code required to do this

  • in C++ Write a program that converts a time in 12-hour format to 24-hour format. The...

    in C++ Write a program that converts a time in 12-hour format to 24-hour format. The program will prompt the user to enter a time in HH:MM:SS AM/PM form. (The time must be entered exactly in this format all on one line.) It will then convert the time to 24 hour form. You may use a string type to read in the entire time at once including the space or you may choose to use separate variables for the hours,...

  • C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it...

    C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the...

  • Determine the time period of the day in C++ a. Request an hour in military time...

    Determine the time period of the day in C++ a. Request an hour in military time between 0 and 23 inclusive. b. If the value for hour is invalid: 1. Output Invalid hour". 2. Repeat the input request. a. Request the minutes b. Print the time using hh:mm format C. Convert to 12 hour format (i.e. AM/PM) and print Use a single if/else-if/else structure to do the following: 1. If the hour is between 0 and 12 inclusive, output "It...

  • 1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice...

    1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the...

  • how do you solve question 69 62. 12.00 63. 12:05 AM State whether am or PM...

    how do you solve question 69 62. 12.00 63. 12:05 AM State whether am or PM is represented by the following times. 66. 0154 67. 1450 68. If a client had IV therapy for 8 hours, ending at 1100, when on the 24-hou was the IV started? State the following completion times as indicated. 69. An IV started at 11:50 PM with an infusion time of 3 hr 30 min (standard time). 70. An IV started at 0025 with an...

  • I need help with a Python program that has error checking loops. Hour must be a...

    I need help with a Python program that has error checking loops. Hour must be a number from 1 to 12. Minute and second must be numbers from 0 to 59. Also check whether “AM” or “PM” is entered. Every time an invalid value is entered, display an error message and ask the user to re-enter a valid value immediately. Functions are not covered yet and are not necessary. Thanks. The following is an example: Enter hour: 0 Hour must...

  • Hey there, I am having a hard time using GUI for c# in visual studio. I...

    Hey there, I am having a hard time using GUI for c# in visual studio. I am making an alarm clock. I have a few questions that I would like to know how to do: --using a button called set time to capture text from a box containing a user-inputted hour, minute, second, and setting the current time to that. --using a button called alarm, Capture the Text in the TextBoxes - the Hours, Minutes, Seconds and Text (am/pm) and...

  • POD 3: Tick Tock Instructions There are 24 hours in a day, but we all think...

    POD 3: Tick Tock Instructions There are 24 hours in a day, but we all think about them differently. Some people and places in the world work with a 24-hour clock that goes from 0:00 to 23:59, while others work with a 12-hour clock that goes from 12.00AM to 11:59PM 24-hour clock 0.00 (midnight) 1200 (noon) 23:59 (one minute before midnight), A COLLAPSE 12-hour clock: *12.00 AM" (midnight) *12:00 PM" (noon) "11:59 PM" (one minute before midnight), You are going...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT