Bird
0
0

You want to improve the watering system to water plants only if soil moisture is below 400 and only between 6 AM and 8 AM. Which Python code snippet correctly adds this time condition?

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

You want to improve the watering system to water plants only if soil moisture is below 400 and only between 6 AM and 8 AM. Which Python code snippet correctly adds this time condition?

import datetime
now = datetime.datetime.now()
soil_moisture = 350
if soil_moisture < 400 and ???:
    print("Watering plants")
else:
    print("No watering")
Anow.hour >= 6 and now.hour <= 8
Bnow.hour > 6 or now.hour < 8
Cnow.hour < 6 and now.hour > 8
Dnow.hour == 6 or now.hour == 8
Step-by-Step Solution
Solution:
  1. Step 1: Understand time range condition

    We want hours from 6 to 8 inclusive, so hour >= 6 and hour <= 8.
  2. Step 2: Combine soil moisture and time conditions

    Use 'and' to check both soil moisture and time range.
  3. Final Answer:

    now.hour >= 6 and now.hour <= 8 -> Option A
  4. Quick Check:

    Check hour between 6 and 8 = B [OK]
Quick Trick: Use 'and' with hour >= 6 and <= 8 for time range [OK]
Common Mistakes:
MISTAKES
  • Using 'or' instead of 'and' for time range
  • Using strict inequalities excluding 6 or 8
  • Checking only exact hours 6 or 8

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes