Consider a drone altitude control function that adjusts altitude based on sensor input. What will be printed after running this code?
def adjust_altitude(current_altitude, target_altitude): if current_altitude < target_altitude: return current_altitude + 1 elif current_altitude > target_altitude: return current_altitude - 1 else: return current_altitude altitude = 10 for _ in range(5): altitude = adjust_altitude(altitude, 12) print(altitude)
Each loop changes altitude by 1 towards the target.
The altitude starts at 10. Loop 1: increases to 11. Loop 2: increases to 12. Loops 3-5: stays at 12. Final printed value is 12.
A drone uses a sensor to maintain altitude. If the sensor reading is below the target altitude, the drone ascends. If above, it descends. Which sensor reading will cause the drone to descend?
Think about when the drone needs to go down.
If the sensor reading is higher than the target, the drone is too high and must descend.
Analyze the following code snippet. What error will it raise when run?
def maintain_altitude(current, target): while current != target: if current < target: current += 1 else: current -= 1 return current print(maintain_altitude(5, 10))
Check the if statement syntax carefully.
The if statement is missing a colon at the end, causing a SyntaxError.
Choose the code snippet that correctly checks if the drone altitude is within safe limits (between 5 and 20 inclusive).
Think about inclusive range checks.
Option B correctly checks if altitude is between 5 and 20 including both ends.
A drone tries to reach target altitude 15 starting from 10. It adjusts altitude by adding half the difference each iteration (rounded down). What is the altitude after 3 iterations?
altitude = 10 for _ in range(3): diff = 15 - altitude altitude += diff // 2 print(altitude)
Calculate each iteration step carefully.
Iteration 1: diff=5, altitude=10+2=12
Iteration 2: diff=3, altitude=12+1=13
Iteration 3: diff=2, altitude=13+1=14
