Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
NumPy - Indexing and Slicing
Identify the error in this code snippet:
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([10, 20, 30, 40])
result = np.where(x > 2, y)
print(result)
ACondition x > 2 is invalid syntax
BArrays x and y have different lengths
CMissing third argument for false condition in np.where()
Dnp.where() cannot be used with arrays
Step-by-Step Solution
Solution:
  1. Step 1: Check np.where() arguments

    np.where() requires three arguments for conditional selection: condition, array if True, array if False.
  2. Step 2: Identify missing argument

    The code only provides condition and one array, missing the third argument for false case.
  3. Final Answer:

    Missing third argument for false condition in np.where() -> Option C
  4. Quick Check:

    np.where needs condition, true array, false array [OK]
Quick Trick: np.where needs three arguments for selection [OK]
Common Mistakes:
  • Using np.where with only two arguments for selection
  • Assuming np.where can infer missing arrays
  • Confusing np.where with np.nonzero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes