LearnFrenzy
Sign in Take a free test

Conditional Statements · Find the Error · Question 6

Error: a variable assigned in only one branch Answer, explanation & discussion

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

The program below stops with an error. Which statement correctly describes the problem?

main.py
marks = 30
if marks >= 40:
    result = "pass"
print("Result:", result)
  • A NameError: result is assigned only when marks >= 40; on this path it was never created Correct
  • B SyntaxError: result must be declared
  • C It prints Result: None
  • D It prints Result: pass
Correct Answer: Option A

Give it a value on every path: add else: result = "fail", or set result = "fail" before the if.

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