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

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.

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

In this one

  • Express half-open intervals
  • Use chained comparisons
  • Check day-part boundaries

How it works

  1. 1

    Read the twenty-four-hour hour.

  2. 2

    Try morning, afternoon, and evening intervals.

  3. 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.