📖 Scenario: You are working on a drone control system. To manage the drone, you need to organize information about its main components and their status.
🎯 Goal: Build a simple program that stores drone components and their status, sets a threshold for battery level, filters components that need attention, and displays the result.
📋 What You'll Learn
Create a dictionary with drone components and their status values
Add a battery level threshold variable
Use a dictionary comprehension to select components below the threshold
Print the filtered components and their status
💡 Why This Matters
🌍 Real World
Drones have many parts that need monitoring to work safely. This project helps organize and check their status easily.
💼 Career
Understanding how to manage and filter data about hardware components is useful for drone programmers and engineers.
Progress0 / 4 steps
1
Create the drone components dictionary
Create a dictionary called components with these exact entries: 'motor': 85, 'battery': 40, 'camera': 90, 'gps': 75, 'sensor': 30
Drone Programming
Hint
Use curly braces {} to create the dictionary and separate each component with a comma.
2
Set the battery level threshold
Create a variable called battery_threshold and set it to 50
Drone Programming
Hint
Just assign the number 50 to the variable battery_threshold.
3
Filter components below the battery threshold
Create a dictionary called low_status_components using a dictionary comprehension that includes only components from components with status less than battery_threshold
Drone Programming
Hint
Use a dictionary comprehension with for name, status in components.items() and an if condition.
4
Display the components needing attention
Write a print statement to display the low_status_components dictionary
Drone Programming
Hint
Use print(low_status_components) to show the filtered dictionary.
Practice
(1/5)
1. Which component of a drone is primarily responsible for controlling its flight and processing sensor data?
easy
A. Motor
B. Battery
C. Camera
D. Flight controller
Solution
Step 1: Understand the role of the flight controller
The flight controller acts like the brain of the drone, managing flight and sensor data.
Step 2: Compare with other components
Batteries provide power, motors move the drone, and cameras capture images, but do not control flight.
2. Which of the following is the correct way to declare a motor speed variable in a drone program using Python?
easy
A. motorSpeed := 100
B. motor-speed = 100
C. motorSpeed = 100
D. motor speed = 100
Solution
Step 1: Check Python variable naming rules
Variable names cannot have spaces or hyphens; they can use letters, numbers, and underscores.
Step 2: Analyze each option
motorSpeed = 100 uses camelCase without spaces or hyphens, which is valid. motor-speed = 100 uses a hyphen, invalid. motor speed = 100 has a space, invalid. motorSpeed := 100 uses walrus operator incorrectly for assignment.
Final Answer:
motorSpeed = 100 -> Option C
Quick Check:
Valid variable names have no spaces or hyphens [OK]
Hint: Variable names: no spaces or hyphens, use letters/numbers/underscores [OK]
Common Mistakes:
Using hyphens in variable names
Including spaces in variable names
Misusing walrus operator for assignment
3. What will be the output of this Python code snippet simulating battery check?
The variable battery_level is 75, which is greater than or equal to 50.
Step 2: Determine which print statement runs
Since 75 >= 50 is true, the code prints 'Battery sufficient'.
Final Answer:
Battery sufficient -> Option B
Quick Check:
75 >= 50 means battery sufficient [OK]
Hint: Check if battery_level >= 50 to decide output [OK]
Common Mistakes:
Confusing comparison operators
Ignoring indentation causing errors
Assuming else runs when condition is true
4. Identify the error in this drone motor control code snippet:
def set_motor_speed(speed):
if speed < 0 or speed > 100:
print('Invalid speed')
else
print(f'Speed set to {speed}')
set_motor_speed(50)
medium
A. Missing colon after else
B. Incorrect function name
C. Invalid comparison operators
D. Wrong print statement syntax
Solution
Step 1: Check syntax after else statement
In Python, the else statement must end with a colon (:).
Step 2: Identify the missing colon
The else line lacks a colon, causing a syntax error.
Final Answer:
Missing colon after else -> Option A
Quick Check:
else must end with ':' in Python [OK]
Hint: Always put ':' after else in Python [OK]
Common Mistakes:
Forgetting colon after else
Misnaming functions
Using wrong comparison signs
5. You want to program a drone to take a photo only if the battery level is above 30% and the camera is ready. Which Python condition correctly checks this before taking a photo?
battery_level = 45
camera_ready = True
if ???:
print('Taking photo')
else:
print('Cannot take photo')
hard
A. battery_level > 30 and camera_ready == True
B. battery_level > 30 or camera_ready == False
C. battery_level <= 30 and camera_ready
D. battery_level >= 30 and not camera_ready
Solution
Step 1: Understand the condition requirements
The drone should take a photo only if battery_level is above 30 and camera is ready (True).
Step 2: Analyze each option
battery_level > 30 and camera_ready == True correctly uses 'and' to require both conditions true. battery_level > 30 or camera_ready == False uses 'or' and wrong camera_ready check. battery_level <= 30 and camera_ready checks battery_level less or equal 30, wrong. battery_level >= 30 and not camera_ready requires camera not ready, wrong.
Final Answer:
battery_level > 30 and camera_ready == True -> Option A
Quick Check:
Both battery > 30 and camera ready needed [OK]
Hint: Use 'and' to require both battery and camera ready [OK]
Common Mistakes:
Using 'or' instead of 'and'
Checking wrong battery comparison
Negating camera_ready incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.