Time Conversion Algorithm

clock

Did You Know?


The 12-hour clock is a time convention in which the 24 hours of the day are divided into two periods: a.m. (from the Latin ante meridiem, meaning “before midday”) and p.m. (post meridiem, “after midday”). Each period consists of 12 hours numbered: 12 (acting as zero), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11. The 24 hour/day cycle starts at 12 midnight (often indicated as 12 a.m.), runs through 12 noon (often indicated as 12 p.m.), and continues to the midnight at the end of the day.

time-conversion-24-12-am-pm

24-hour to 12-hour clock convertor


Your challenge consists of writing a computer program that asks the end-user to enter a time in the 24-hour format (e.g. 18:36). The program will convert this time in the 12-hour format (e.g. 6:36 PM).

To do so you can base your Python code on the following flowchart:
time-conversion-flowchart

Python Code


Testing


Once your code is done, complete the following tests to check that your code is working as it should:

Test # Input Values Expected Output Actual Output
#1 06:30 6:30 am
#2 14:25 2:25 pm
#3 00:52 12:52 am
#4 11:59 11:59 am
#5 12:00 12:00 pm
#6 23:59 11:59 pm

Extension


warning-signAmend this code to validate the user entry and make sure they enter a time between 00:00 and 23:59 and to display a meaningful error message if not.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: