D. Using assignment '=' instead of comparison '==' in if condition
Solution
Step 1: Check the if condition syntax
The condition uses '=' which assigns value, but it should compare using '=='.
Step 2: Verify other parts of the code
Print statements have parentheses, function is called, and variable is defined correctly.
Final Answer:
Using assignment '=' instead of comparison '==' in if condition -> Option D
Quick Check:
Use '==' for comparison in if statements [OK]
Hint: Use '==' to compare, '=' is for assignment [OK]
Common Mistakes:
Confusing '=' with '==' in conditions
Forgetting to call the function
Missing parentheses in print (Python 3)
5. You want a drone to scan multiple zones and report all found targets with their locations. Which code snippet correctly collects and returns all found targets in a dictionary with zone names as keys?
Options:
A)
def scan_zones(zones):
results = {}
for zone in zones:
found = scan(zone)
if found:
results[zone] = found
return results
B)
def scan_zones(zones):
results = []
for zone in zones:
found = scan(zone)
if found:
results.append({zone: found})
return results
C)
def scan_zones(zones):
results = {}
for zone in zones:
found = scan(zone)
results[zone] = found
return results
D)
def scan_zones(zones):
results = {}
for zone in zones:
if scan(zone):
results.append(zone)
return results
hard
A. Collects found targets in a list of dictionaries, not a single dict
B. Collects found targets in a dictionary with zone keys correctly
C. Adds all zones including those without targets (found may be False)
D. Uses append on a dictionary, which causes an error
Solution
Step 1: Understand the goal
We want a dictionary with zones as keys and found targets as values, only if found.
Step 2: Analyze each option
The snippet that adds only found targets to a dict is correct; the one that uses a list of dicts produces the wrong structure; the one that adds all zones includes those without targets; the one that tries to append to a dict causes an error.
Final Answer:
Collects found targets in a dictionary with zone keys correctly -> Option B
Quick Check:
Dict with keys for found targets only = Collects found targets in a dictionary with zone keys correctly [OK]
Hint: Use dict and add only found targets with keys [OK]
Common Mistakes:
Adding all zones even if no target found
Using list instead of dictionary for key-value pairs
Trying to append to a dictionary (invalid)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.