B. IndentationError: Incorrect indentation in function
C. TypeError: Cannot assign boolean to dictionary key
D. SyntaxError: Use '==' for comparison instead of '='
Solution
Step 1: Identify the error in the if condition
The code uses '=' which is assignment, not comparison. This causes a SyntaxError.
Step 2: Correct the comparison operator
Replace '=' with '==' to compare drone_state['armed'] to True.
Final Answer:
SyntaxError: Use '==' for comparison instead of '=' -> Option D
Quick Check:
Comparison needs '==' not '=' [OK]
Hint: Use '==' to compare values, '=' to assign [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Assuming dictionary key is missing
Confusing indentation errors with syntax errors
5. You want to write a function toggle_arm() that arms the drone if disarmed, and disarms if armed. Given drone_state = {'armed': False}, which code correctly implements this behavior?
hard
A. def toggle_arm():
drone_state['armed'] = not drone_state['armed']
return 'Drone armed' if drone_state['armed'] else 'Drone disarmed'
B. def toggle_arm():
if drone_state['armed'] == False:
drone_state['armed'] = False
return 'Drone disarmed'
else:
drone_state['armed'] = True
return 'Drone armed'
C. def toggle_arm():
if not drone_state['armed']:
drone_state['armed'] = False
return 'Drone disarmed'
else:
drone_state['armed'] = True
return 'Drone armed'
Toggle means switch state: if armed, disarm; if disarmed, arm.
Step 2: Check each option's logic
def toggle_arm():
drone_state['armed'] = not drone_state['armed']
return 'Drone armed' if drone_state['armed'] else 'Drone disarmed' uses 'not' to flip boolean and returns correct message based on new state.
Final Answer:
def toggle_arm():
drone_state['armed'] = not drone_state['armed']
return 'Drone armed' if drone_state['armed'] else 'Drone disarmed' -> Option A
Quick Check:
Toggle by flipping boolean with 'not' [OK]
Hint: Use 'not' to flip armed state quickly [OK]
Common Mistakes:
Not flipping the state correctly
Returning wrong message after toggle
Code after return statement ignored
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.