Time-based Greeting
Choose a greeting from a twenty-four-hour clock value. Beginners practice Python chained comparisons and trace which interval includes each boundary hour.
1# Take hour as input2hour = int(input("Enter hour (0-23): "))34# Determine greeting based on hour5if 5 <= hour < 12:6 print("Good Morning")7elif 12 <= hour < 17:8 print("Good Afternoon")9elif 17 <= hour < 21:10 print("Good Evening")11else:12 print("Good Night")
Output
Enter hour (0-23): 10 Good Morning Enter hour (0-23): 15 Good Afternoon Enter hour (0-23): 20 Good Evening Enter hour (0-23): 22 Good Night
In this one
- Express half-open intervals
- Use chained comparisons
- Check day-part boundaries
How it works
- 1
Read the twenty-four-hour hour.
- 2
Try morning, afternoon, and evening intervals.
- 3
Use Good Night when none matches.
Edge cases
What is printed at hour 5?
What is printed at hour 5?
Good Morning; the lower endpoint is included.
Is hour 25 rejected?
Is hour 25 rejected?
No. It reaches Good Night, so validation would be needed in an actual clock tool.
What's going on
The day is split into morning, afternoon, evening, and a remaining night period. Each chained comparison describes a lower bound that is included and an upper bound that is excluded.
At 12, the morning test fails and the afternoon test succeeds. At 21, the evening interval has ended, so Good Night is printed. The final else also catches invalid hours because the code assumes the input follows its prompt.
What is printed at hour 5? Good Morning; the lower endpoint is included.
Is hour 25 rejected? No. It reaches Good Night, so validation would be needed in an actual clock tool.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which interval means afternoon?
Pick an answer to check it.
Question 2
What is printed at hour 5?
Pick an answer to check it.
Questions people ask
What is printed at hour 5?
Good Morning; the lower endpoint is included.
Is hour 25 rejected?
No. It reaches Good Night, so validation would be needed in an actual clock tool.
Which interval means afternoon?
12 <= hour < 17.
๐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