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 input
2hour = int(input("Enter hour (0-23): "))
3
4# Determine greeting based on hour
5if 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