Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Email alerts on sensor thresholds in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sensor Alert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output when sensor value exceeds threshold?
Consider this Python code snippet running on a Raspberry Pi that reads a temperature sensor and sends an email alert if the temperature exceeds 30°C. What will be printed if the sensor reads 32°C?
Raspberry Pi
temperature = 32
threshold = 30
if temperature > threshold:
    print("Alert: Temperature too high!")
else:
    print("Temperature is normal.")
AAlert: Temperature too high!
BTemperature is normal.
CNo output
DError: sensor not found
Attempts:
2 left
💡 Hint
Check the condition comparing temperature and threshold.
🧠 Conceptual
intermediate
1:00remaining
Which component is essential for sending email alerts from Raspberry Pi?
To send email alerts when a sensor threshold is crossed on a Raspberry Pi, which of the following is essential?
ASMTP server configuration
BGPIO pin setup only
CBluetooth pairing
DLCD display connection
Attempts:
2 left
💡 Hint
Think about how emails are sent programmatically.
🔧 Debug
advanced
2:30remaining
Why does this email alert code fail to send?
This Python code snippet is intended to send an email alert when the sensor value is above threshold. Why does it fail to send the email?
Raspberry Pi
import smtplib
sensor_value = 45
threshold = 40
if sensor_value > threshold:
    server = smtplib.SMTP('smtp.example.com', 587)
    server.ehlo()
    server.starttls()
    server.login('user@example.com', 'password')
    message = 'Subject: Alert\n\nSensor value exceeded threshold.'
    server.sendmail('user@example.com', 'alert@example.com', message)
    server.quit()
AIncorrect SMTP port number
BMissing server.ehlo() call before starttls()
CNo subject line in the message
DUsing starttls() without login
Attempts:
2 left
💡 Hint
Check the SMTP protocol sequence for secure connection.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this sensor alert code
What is the syntax error in the following Python code that reads a sensor and sends an alert?
Raspberry Pi
temperature = 28
threshold = 25
if temperature > threshold:
    print('Alert: High temperature!')
AVariable names not defined
BIncorrect indentation of print statement
CUsing single quotes instead of double quotes
DMissing colon ':' after if condition
Attempts:
2 left
💡 Hint
Check the if statement syntax carefully.
🚀 Application
expert
2:00remaining
How many email alerts are sent by this code?
This Python code reads sensor values from a list and sends an email alert for each value above 50. How many emails will be sent?
Raspberry Pi
sensor_values = [45, 52, 60, 48, 55]
threshold = 50
alerts_sent = 0
for value in sensor_values:
    if value > threshold:
        # send email alert (code omitted)
        alerts_sent += 1
print(alerts_sent)
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Count how many values in the list are greater than 50.

Practice

(1/5)
1. What is the main purpose of sending email alerts when a sensor value crosses a threshold on a Raspberry Pi?
easy
A. To store sensor data permanently in a database
B. To notify the user immediately about important sensor events
C. To increase the sensor's measurement accuracy
D. To reset the sensor automatically

Solution

  1. Step 1: Understand the role of email alerts

    Email alerts are used to inform users instantly when something important happens, like a sensor crossing a limit.
  2. Step 2: Compare options with this purpose

    Only To notify the user immediately about important sensor events matches this purpose. Options B, C, and D describe unrelated tasks.
  3. Final Answer:

    To notify the user immediately about important sensor events -> Option B
  4. Quick Check:

    Email alerts = notify user instantly [OK]
Hint: Email alerts are for instant notifications, not data storage [OK]
Common Mistakes:
  • Confusing alerts with data logging
  • Thinking alerts improve sensor accuracy
  • Assuming alerts reset hardware automatically
2. Which of the following Python code snippets correctly sends an email alert when a sensor value exceeds 50?
easy
A. if sensor_value > 50: send_email_alert()
B. if sensor_value < 50 send_email_alert()
C. if sensor_value > 50 send_email_alert()
D. if sensor_value > 50: send_email_alert

Solution

  1. Step 1: Check Python if-statement syntax

    Python requires a colon after the if condition and indentation for the block.
  2. Step 2: Validate each option

    if sensor_value > 50: send_email_alert() has correct syntax and logic. if sensor_value < 50 send_email_alert() misses colon and wrong condition. if sensor_value > 50 send_email_alert() misses colon and indentation. if sensor_value > 50: send_email_alert misses parentheses for function call.
  3. Final Answer:

    if sensor_value > 50: send_email_alert() -> Option A
  4. Quick Check:

    Correct if syntax + function call = if sensor_value > 50: send_email_alert() [OK]
Hint: Remember colon and indentation after if, and parentheses for function calls [OK]
Common Mistakes:
  • Missing colon after if condition
  • Forgetting parentheses when calling functions
  • Incorrect indentation causing syntax errors
3. Given the code below, what will be printed if sensor_value = 75?
def check_sensor(value):
    if value > 70:
        print("Alert: High sensor value!")
    else:
        print("Sensor value normal.")

check_sensor(sensor_value)
medium
A. Alert: High sensor value!
B. No output
C. Sensor value normal.
D. SyntaxError

Solution

  1. Step 1: Analyze the condition with sensor_value = 75

    Since 75 > 70, the if condition is true.
  2. Step 2: Determine which print statement runs

    The code prints "Alert: High sensor value!" because the if condition is met.
  3. Final Answer:

    Alert: High sensor value! -> Option A
  4. Quick Check:

    75 > 70 triggers alert print [OK]
Hint: Check if condition true or false to find printed message [OK]
Common Mistakes:
  • Confusing else output with if output
  • Assuming no output if condition true
  • Thinking code has syntax errors
4. The following code is intended to send an email alert if the sensor value is above 60, but it does not work. What is the error?
sensor_value = 65
if sensor_value > 60
    send_email_alert()
medium
A. send_email_alert() is not defined
B. Indentation is incorrect
C. sensor_value should be a string
D. Missing colon after the if condition

Solution

  1. Step 1: Check syntax of the if statement

    The if statement is missing a colon at the end of the condition line.
  2. Step 2: Identify the cause of failure

    Without the colon, Python raises a syntax error and the code won't run.
  3. Final Answer:

    Missing colon after the if condition -> Option D
  4. Quick Check:

    if statement requires colon [:] [OK]
Hint: Always put a colon after if conditions in Python [OK]
Common Mistakes:
  • Forgetting colon after if condition
  • Assuming function is undefined without checking syntax first
  • Thinking sensor_value must be string
5. You want to send an email alert only once when the sensor value crosses above 80, not repeatedly while it stays above 80. Which approach is best to implement this on a Raspberry Pi?
hard
A. Restart the Raspberry Pi after each alert to reset the sensor
B. Send an email every time the sensor value is checked and above 80
C. Use a flag variable to track if alert was already sent and reset it when value goes below 80
D. Ignore sensor values below 80 and send emails continuously

Solution

  1. Step 1: Understand the problem of repeated alerts

    Sending emails repeatedly while the value stays high can spam the user.
  2. Step 2: Use a flag to send alert only once

    A flag variable remembers if alert was sent; reset it when value drops below threshold to allow future alerts.
  3. Final Answer:

    Use a flag variable to track if alert was already sent and reset it when value goes below 80 -> Option C
  4. Quick Check:

    Flag variable prevents repeated alerts [OK]
Hint: Track alert state with a flag to avoid repeated emails [OK]
Common Mistakes:
  • Sending emails every check causes spam
  • Restarting device is inefficient and unnecessary
  • Ignoring values below threshold misses reset opportunity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard