Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

AM or PM Check

Determine AM or PM from a twenty-four-hour time in Python. Students inspect the noon boundary and notice that the entered minutes do not affect this label.

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

In this one

  • Interpret a twenty-four-hour hour
  • Handle noon
  • Notice unused input

How it works

  1. 1

    Read hours and minutes as integers.

  2. 2

    Test whether hours is below twelve.

  3. 3

    Print AM there and PM in the remaining branches.

Edge cases

What does hour 0 mean here?

What does hour 0 mean here?

It belongs to AM.

Do minutes change AM to PM?

Do minutes change AM to PM?

No. Only hours is checked in this program.

What's going on

AM covers hours zero through eleven in this model. Noon, hour twelve, begins PM. The program reads minutes too, but never uses them in the decision.

Both the hours == 12 branch and the final else print PM; the extra branch is educational rather than necessary. Input 10:30 prints AM, while 15:45 prints PM. The code trusts the stated hour and minute ranges.

What does hour 0 mean here? It belongs to AM.

Do minutes change AM to PM? No. Only hours is checked in this program.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is printed at hour 12?

Pick an answer to check it.

Question 2

What does hour 0 mean here?

Pick an answer to check it.

Questions people ask

What does hour 0 mean here?

It belongs to AM.

Do minutes change AM to PM?

No. Only hours is checked in this program.

What is printed at hour 12?

PM.