nerdexam
Python_Institute

PCEP-30-02 · Question #252

The following code reads two numbers. Which of the following is the correct input for the code? 1 x, y = eval(input('Enter two numbers: ')) 2 print(x) 3 print(y)

The correct answer is C. <pre>3 4</pre>. Option C is correct because eval(input(...)) evaluates the user's typed string as a Python expression - when the user types 3,4, Python's eval() interprets it as a tuple (3, 4), which can then be unpacked into x and y via tuple assignment. Option B (3 4) fails because eval('3…

Question

The following code reads two numbers. Which of the following is the correct input for the code? 1 x, y = eval(input('Enter two numbers: ')) 2 print(x) 3 print(y)

Options

  • A3,4
  • B3 4
  • C<pre>3 4</pre>
  • DNone of the above.

How the community answered

(31 responses)
  • A
    3% (1)
  • B
    10% (3)
  • C
    71% (22)
  • D
    16% (5)

Explanation

Option C is correct because eval(input(...)) evaluates the user's typed string as a Python expression - when the user types 3,4, Python's eval() interprets it as a tuple (3, 4), which can then be unpacked into x and y via tuple assignment. Option B (3 4) fails because eval('3 4') raises a SyntaxError - two numbers separated by a space is not valid Python syntax. Option A may appear similar but lacks the comma that signals tuple creation to the Python parser, or represents a format eval() cannot unpack into two variables. Option D is incorrect because a valid input format does exist.

Memory tip: Think of eval() as "paste this into Python and run it" - so your input must be valid Python code. A comma between two numbers creates a tuple in Python, which is exactly what tuple unpacking (x, y = ...) needs.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice