PPA-3
Question
Accept an integer as input and print the time of the day. Use the following table for reference.
Input | Output |
---|---|
\(T < 0\) | INVALID |
\(0 \leq T \leq 5\) | NIGHT |
\(6 \leq T \leq 11\) | MORNING |
\(12 \leq T \leq 17\) | AFTERNOON |
\(18 \leq T \leq 23\) | EVENING |
\(T \geq 24\) | INVALID |
The input will be a single line containing an integer. The output should be one of these strings: NIGHT, MORNING, AFTERNOON, EVENING, INVALID.
Hint
How is this problem related to the previous problem? Do they have the same structure?
Think about the following code snippets:
Come up with two different solutions to the problem, one that develops snippet-1 and the other which develops snippet-2. What is the difference between these two solutions that you arrive at? Which one is better?
Solutions
It is better to stick to if-elif-else
ladders wherever possible and avoid a sequence of if
conditions. The if-elif-else
ladder is more efficient. As soon as one of the conditions is satisfied, the control will exit from the ladder. An if-if-if
ladder on the other hand will end up checking every one of the if
conditions for every possible input.