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.
1# Take time2hours = int(input("Enter hours (0-23): "))3minutes = int(input("Enter minutes (0-59): "))45# Determine AM or PM6if 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
Read hours and minutes as integers.
- 2
Test whether hours is below twelve.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum