Bird
0
0

You want to modify your automated watering system to water plants only between 6 AM and 8 AM if soil is dry. Which Python code snippet correctly adds this time condition?

hard🚀 Application Q8 of 15
Raspberry Pi - Automation and Scheduling

You want to modify your automated watering system to water plants only between 6 AM and 8 AM if soil is dry. Which Python code snippet correctly adds this time condition?

Afrom datetime import datetime if datetime.hour >= 6 and datetime.hour <= 8 and moisture < threshold: GPIO.output(pump_pin, GPIO.HIGH)
Bfrom datetime import datetime now = datetime.now() if 6 <= now.hour <= 8 and moisture < threshold: GPIO.output(pump_pin, GPIO.HIGH)
Cfrom datetime import time if time.now().hour >= 6 and time.now().hour <= 8 and moisture < threshold: GPIO.output(pump_pin, GPIO.HIGH)
Dimport time if time.hour >= 6 and time.hour <= 8 and moisture < threshold: GPIO.output(pump_pin, GPIO.HIGH)
Step-by-Step Solution
Solution:
  1. Step 1: Import and get current time correctly

    Use datetime.now() to get current time object.
  2. Step 2: Check hour attribute and combine with moisture condition

    Check if hour is between 6 and 8 inclusive and moisture below threshold.
  3. Final Answer:

    Correct datetime usage with hour check and moisture condition -> Option B
  4. Quick Check:

    Correct time check = from datetime import datetime now = datetime.now() if 6 <= now.hour <= 8 and moisture < threshold: GPIO.output(pump_pin, GPIO.HIGH) [OK]
Quick Trick: Use datetime.now().hour for current hour checks [OK]
Common Mistakes:
MISTAKES
  • Using time module incorrectly
  • Calling datetime.hour without instance
  • Importing wrong classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes