Question 2
Back to Exercise
Python refuses to run the program below. Which statement correctly describes the problem?
nums = [3, -1, 4]
positives = [n if n > 0 for n in nums]
print(positives)
- A Negative numbers are not allowed
- B n must be declared first
- C An if before for is a conditional expression and needs an else; to filter, move it after for: [n for n in nums if n > 0] Correct
- D The list needs round brackets
Correct Answer: Option C
The two positions mean different things, and each has its own fixed syntax.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!