Time-based Greeting
Take the hour of the day (0-23) and print "Good Morning", "Good Afternoon", "Good Evening", or "Good Night".
IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 5 of 10
time-based-greeting.py
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
What's going on
Use range conditions to determine time of day.
Key Concepts:
Morning: 5 to 11 (5 <= hour < 12)
Afternoon: 12 to 16 (12 <= hour < 17)
Evening: 17 to 20 (17 <= hour < 21)
Night: all other hours
🔗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