PCEP-30-02 · Question #259
You want to print the sum of two number. What snippet would you insert in the line indicated below: 1 x = input('Enter the first number: ') 2 y = input('Enter the second number: ') 3 # insert your…
The correct answer is D. print('The Result is ' + str(int(x) + int(y))). Option D correctly wraps the addition in str() - converting both inputs to integers with int(x) and int(y), adding them numerically, then converting the result back to a string for concatenation with the label. Option A fails because it tries to concatenate a string ('The…
Question
Options
- Aprint('The Result is ' + (int(x) + int(y)))
- Bprint('The Result is ' + str(int(x) + int(y)))
- Cprint('The Result is ' + str(int(x + y)))
- Dprint('The Result is ' + str(int(x) + int(y)))
How the community answered
(51 responses)- A2% (1)
- B10% (5)
- C4% (2)
- D84% (43)
Explanation
Option D correctly wraps the addition in str() - converting both inputs to integers with int(x) and int(y), adding them numerically, then converting the result back to a string for concatenation with the label.
Option A fails because it tries to concatenate a string ('The Result is ') directly with an integer expression (int(x) + int(y)) - Python raises a TypeError when you do str + int.
Option C is the classic trap: int(x + y) adds x and y as strings first (e.g., '5' + '3' becomes '53'), then converts that concatenated string to an integer - giving 53 instead of 8.
Options B and D appear identical in this question, which is likely a deliberate distractor to test whether you're reading carefully; both express the correct logic (str(int(x) + int(y))), and D is designated as the keyed answer.
Memory tip: Think "inside-out" - convert to the working type first (int), do the math, then convert to the output type (str) last. The order is: in → operate → out.
Community Discussion
No community discussion yet for this question.