AM or PM Check

Take 24-hour time (hours and minutes) and print whether it is AM or PM.

IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 4 of 10
am-or-pm-check.py
1# Take time
2hours = int(input("Enter hours (0-23): "))
3minutes = int(input("Enter minutes (0-59): "))
4
5# Determine AM or PM
6if hours < 12:
7 print("AM")
8elif hours == 12:
9 print("PM")
10else:
11 print("PM")

Output

Enter hours (0-23): 10
Enter minutes (0-59): 30
AM

Enter hours (0-23): 15
Enter minutes (0-59): 45
PM

What's going on

Check hour value to determine AM/PM.

Key Concepts:

AM: hours 0-11
PM: hours 12-23
12:00 is PM