📖 Scenario: You are programming a drone to maintain a safe flying altitude. The drone reads altitude data from its sensors and adjusts its height to stay within a safe range.
🎯 Goal: Build a simple altitude control program that reads altitude data, sets a safe altitude range, checks if the drone is too low or too high, and prints the appropriate action.
📋 What You'll Learn
Create a variable to store the current altitude reading.
Create variables for minimum and maximum safe altitude.
Write logic to check if the altitude is below minimum, above maximum, or safe.
Print the action the drone should take based on altitude.
💡 Why This Matters
🌍 Real World
Drones must keep a safe altitude to avoid obstacles and follow regulations. This program helps drones decide when to go up, down, or stay steady.
💼 Career
Understanding sensor data and control logic is key for drone programming and robotics jobs.
Progress0 / 4 steps
1
Set the current altitude
Create a variable called current_altitude and set it to 120 to represent the drone's current altitude in meters.
Drone Programming
Hint
Use the equals sign = to assign the value 120 to the variable current_altitude.
2
Set safe altitude range
Create two variables called min_altitude and max_altitude. Set min_altitude to 100 and max_altitude to 150 to define the safe flying range in meters.
Drone Programming
Hint
Assign 100 to min_altitude and 150 to max_altitude using =.
3
Check altitude and decide action
Write an if-elif-else statement to check the current_altitude. If it is less than min_altitude, set a variable action to "Ascend". If it is greater than max_altitude, set action to "Descend". Otherwise, set action to "Hold".
Drone Programming
Hint
Use if, elif, and else to compare current_altitude with min_altitude and max_altitude.
4
Print the drone action
Write a print statement to display the text "Drone action:" followed by the value of the action variable.
Drone Programming
Hint
Use print("Drone action:", action) to show the result.
Practice
(1/5)
1. What is the main purpose of altitude control in drone programming?
easy
A. To keep or change the drone's height safely
B. To control the drone's speed horizontally
C. To manage the drone's battery life
D. To adjust the drone's camera angle
Solution
Step 1: Understand altitude control function
Altitude control is about managing the drone's height above ground.
Step 2: Compare options to this function
Only To keep or change the drone's height safely talks about height control, others focus on speed, battery, or camera.
Final Answer:
To keep or change the drone's height safely -> Option A
Quick Check:
Altitude control = height management [OK]
Hint: Altitude control means managing drone height safely [OK]
Common Mistakes:
Confusing altitude with speed control
Thinking altitude control manages battery
Mixing altitude control with camera settings
2. Which of the following is the correct syntax to set a target altitude of 100 meters in a drone program?
easy
A. altitude = 100m
B. setAltitude(100)
C. targetAltitude = '100'
D. set_altitude = 100
Solution
Step 1: Identify correct function call syntax
Function calls use parentheses with numeric values, like setAltitude(100).
Step 2: Check other options for syntax errors
altitude = 100m uses invalid unit '100m' without quotes or function; C uses string instead of number; D assigns value but not a function call.
Final Answer:
setAltitude(100) -> Option B
Quick Check:
Function call with number = setAltitude(100) [OK]
Hint: Function calls use parentheses with numbers, not strings or units [OK]
Common Mistakes:
Using units like '100m' directly in code
Assigning string instead of number for altitude
Confusing assignment with function call
3. What will be the output of this code snippet controlling drone altitude?
current_altitude is 50, set_target is 100, so 50 < 100 is True.
Step 2: Determine which print runs
Since condition is True, it prints 'Ascending'.
Final Answer:
Ascending -> Option C
Quick Check:
50 < 100 means Ascending [OK]
Hint: If current altitude less than target, drone ascends [OK]
Common Mistakes:
Confusing less than with greater than
Expecting 'Holding position' when ascending
Thinking code has syntax error
4. Identify the error in this altitude control code snippet:
def adjust_altitude(current, target):
if current > target
return 'Descending'
else:
return 'Ascending'
medium
A. Indentation error in else block
B. Wrong comparison operator
C. Function missing return statement
D. Missing colon after if condition
Solution
Step 1: Check syntax of if statement
The if line lacks a colon at the end, which is required in Python.
Step 2: Verify other parts
Comparison operator and return statements are correct; indentation is consistent.
Final Answer:
Missing colon after if condition -> Option D
Quick Check:
Python if needs colon ':' [OK]
Hint: Python if statements always end with a colon ':' [OK]
Common Mistakes:
Forgetting colon after if condition
Assuming wrong operator causes error
Misreading indentation as error
5. You want the drone to hover steadily at 120 meters. Which code snippet correctly adjusts motor power based on current altitude to maintain this target?
def control_altitude(current):
target = 120
if current < target:
power = 70
elif current > target:
power = 30
else:
power = 50
return power
print(control_altitude(120))
hard
A. 50
B. 70
C. 30
D. 120
Solution
Step 1: Understand the target and current altitude
Target is 120 meters; current altitude passed is 120.
Step 2: Follow the if-elif-else logic
Since current equals target, the else block runs, setting power to 50.
Final Answer:
50 -> Option A
Quick Check:
Hover power at target altitude = 50 [OK]
Hint: At target altitude, motor power is set to hover value [OK]
Common Mistakes:
Choosing power for ascending or descending instead of hover
Confusing comparison operators
Returning altitude instead of power
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.