Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Sensors Provide Situational Awareness
📖 Scenario: You are programming a drone that needs to understand its surroundings to fly safely. Sensors on the drone collect data about obstacles and distances. This helps the drone know what is around it, which is called situational awareness.
🎯 Goal: Build a simple program that uses sensor data to create a map of nearby obstacles and shows which areas are safe to fly.
📋 What You'll Learn
Create a dictionary called sensor_readings with exact obstacle distances
Create a variable called safe_distance to set the minimum safe flying distance
Use a dictionary comprehension to create safe_zones showing which directions are safe
Print the safe_zones dictionary to display the drone's situational awareness
💡 Why This Matters
🌍 Real World
Drones use sensors to detect obstacles and avoid crashes by understanding their environment.
💼 Career
Programming drones to interpret sensor data is key for roles in robotics, automation, and unmanned vehicle development.
Progress0 / 4 steps
1
Set up sensor data
Create a dictionary called sensor_readings with these exact entries: 'front': 5, 'left': 2, 'right': 7, 'back': 3 representing distances to obstacles in meters.
Drone Programming
Hint
Think of the drone's four directions and how far obstacles are in each.
2
Set the safe flying distance
Create a variable called safe_distance and set it to 4 to represent the minimum safe distance in meters.
Drone Programming
Hint
This number tells the drone how close is too close to an obstacle.
3
Determine safe zones
Use a dictionary comprehension to create a new dictionary called safe_zones. It should have the same keys as sensor_readings, and the value should be True if the distance is greater than or equal to safe_distance, otherwise False.
Drone Programming
Hint
Check each direction's distance and mark it True if safe, False if not.
4
Show the drone's situational awareness
Print the safe_zones dictionary to display which directions are safe to fly.
Drone Programming
Hint
Use print(safe_zones) to show the results.
Practice
(1/5)
1. Why do sensors provide situational awareness to drones?
easy
A. They allow drones to change color mid-flight.
B. They make drones fly faster without control.
C. They help drones detect obstacles and navigate safely.
D. They increase the drone's battery life automatically.
Solution
Step 1: Understand the role of sensors in drones
Sensors collect information about the drone's environment, like obstacles or weather.
Step 2: Connect sensor data to drone safety
Using sensor data, drones can avoid collisions and navigate safely.
Final Answer:
They help drones detect obstacles and navigate safely. -> Option C
Quick Check:
Sensors = Safe navigation [OK]
Hint: Sensors detect surroundings to keep drones safe [OK]
Common Mistakes:
Thinking sensors control speed directly
Believing sensors change drone color
Assuming sensors improve battery life
2. Which of the following is the correct way to read a sensor value in drone programming?
easy
A. sensorValue == readSensor()
B. sensorValue = readSensor()
C. readSensor = sensorValue()
D. sensorValue : readSensor()
Solution
Step 1: Identify correct assignment syntax
In programming, '=' assigns a value to a variable, so sensorValue = readSensor() is correct.
Step 2: Check other options for errors
'==' is comparison, not assignment; ':' is invalid here; swapping function and variable is wrong.
Final Answer:
sensorValue = readSensor() -> Option B
Quick Check:
Assignment uses '=' not '==' [OK]
Hint: Use '=' to assign sensor data to a variable [OK]
Common Mistakes:
Using '==' instead of '=' for assignment
Swapping variable and function names
Using ':' instead of '='
3. What will this code print if the sensor detects an obstacle at distance 5?
distance = getSensorDistance()
if distance < 10:
print("Obstacle detected")
else:
print("Path is clear")
medium
A. Obstacle detected
B. No output
C. Error: invalid syntax
D. Path is clear
Solution
Step 1: Understand the sensor value and condition
The sensor returns distance = 5, which is less than 10.
Step 2: Evaluate the if condition
Since 5 < 10 is true, the code prints "Obstacle detected".
Final Answer:
Obstacle detected -> Option A
Quick Check:
5 < 10 triggers obstacle message [OK]
Hint: Check if sensor value meets condition to decide output [OK]
Common Mistakes:
Confusing '<' with '>' in condition
Assuming syntax error due to '<' symbol
Ignoring indentation rules
4. Find the error in this code snippet that reads sensor data and prints a warning:
sensorValue = readSensor()
if sensorValue > 20
print("Warning: High value")
medium
A. print statement should be outside if block
B. Incorrect function name readSensor()
C. sensorValue should be a string
D. Missing colon ':' after if condition
Solution
Step 1: Check syntax of if statement
The if statement must end with a colon ':' to be valid syntax.
Step 2: Verify other parts of code
Function name and print placement are correct; sensorValue can be any type supporting '>' operator.
Final Answer:
Missing colon ':' after if condition -> Option D
Quick Check:
if statements need ':' [OK]
Hint: Always put ':' after if conditions [OK]
Common Mistakes:
Forgetting ':' after if condition
Thinking print must be outside if
Assuming function name is wrong without context
5. You want a drone to stop immediately if any sensor detects an obstacle closer than 3 meters. Which code snippet correctly uses multiple sensors to provide this situational awareness?
hard
A. if sensor1.getDistance() < 3 or sensor2.getDistance() < 3 or sensor3.getDistance() < 3:
drone.stop()
B. if sensor1.getDistance() > 3 and sensor2.getDistance() > 3 and sensor3.getDistance() > 3:
drone.stop()
C. if sensor1.getDistance() == 3 or sensor2.getDistance() == 3 or sensor3.getDistance() == 3:
drone.stop()
D. if sensor1.getDistance() < 3 and sensor2.getDistance() < 3 and sensor3.getDistance() < 3:
drone.stop()
Solution
Step 1: Understand the stopping condition
The drone should stop if any sensor detects an obstacle closer than 3 meters.
Step 2: Analyze logical operators in options
if sensor1.getDistance() < 3 or sensor2.getDistance() < 3 or sensor3.getDistance() < 3:
drone.stop() uses 'or' to check if any sensor is less than 3, which matches the requirement.
Step 3: Check other options
if sensor1.getDistance() > 3 and sensor2.getDistance() > 3 and sensor3.getDistance() > 3:
drone.stop() stops if all sensors are greater than 3 (wrong), C stops only if distance equals 3 (too strict), D stops only if all sensors are less than 3 (too strict).
Final Answer:
if sensor1.getDistance() < 3 or sensor2.getDistance() < 3 or sensor3.getDistance() < 3:
drone.stop() -> Option A
Quick Check:
Any sensor < 3 triggers stop [OK]
Hint: Use 'or' to stop if any sensor detects close obstacle [OK]
Common Mistakes:
Using 'and' instead of 'or' for any sensor condition