Bird
0
0

You want to print 'Good morning' if the hour is less than 12, 'Good afternoon' if hour is between 12 and 18 (inclusive), and 'Good evening' otherwise. Which code correctly uses conditional statements?

hard📝 Application Q15 of 15
Python - Conditional Statements
You want to print 'Good morning' if the hour is less than 12, 'Good afternoon' if hour is between 12 and 18 (inclusive), and 'Good evening' otherwise. Which code correctly uses conditional statements?
Aif hour < 12: print('Good morning') elif hour > 12 and hour < 18: print('Good afternoon') else: print('Good evening')
Bif hour < 12: print('Good morning') if 12 <= hour <= 18: print('Good afternoon') else: print('Good evening')
Cif hour < 12: print('Good morning') else if 12 <= hour <= 18: print('Good afternoon') else: print('Good evening')
Dif hour < 12: print('Good morning') elif 12 <= hour <= 18: print('Good afternoon') else: print('Good evening')
Step-by-Step Solution
Solution:
  1. Step 1: Check correct use of if, elif, and else

    if hour < 12: print('Good morning') elif 12 <= hour <= 18: print('Good afternoon') else: print('Good evening') uses elif correctly and covers all hour ranges without overlap.
  2. Step 2: Identify errors in other options

    if hour < 12: print('Good morning') if 12 <= hour <= 18: print('Good afternoon') else: print('Good evening') uses two separate if statements causing multiple prints; if hour < 12: print('Good morning') else if 12 <= hour <= 18: print('Good afternoon') else: print('Good evening') uses invalid 'else if' syntax; if hour < 12: print('Good morning') elif hour > 12 and hour < 18: print('Good afternoon') else: print('Good evening') misses equality for 12 and 18 in afternoon range.
  3. Final Answer:

    Code with proper if, elif, and else and correct ranges -> Option D
  4. Quick Check:

    Use elif for multiple conditions [OK]
Quick Trick: Use elif for multiple exclusive conditions [OK]
Common Mistakes:
MISTAKES
  • Using 'else if' instead of 'elif'
  • Using multiple separate if causing multiple outputs
  • Incorrect range checks missing equal signs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes