LearnFrenzy
Sign in Take a free test

List Comprehensions · Find the Error · Question 2

Error: a conditional expression without else Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links