PCEP-30-02 · Question #277
You want the name the user puts in to be written back to the monitor. What snippet would you insert in the line indicated below: print('Enter Your Name: ') insert your code here print(name)
The correct answer is C. name = input('name'). Option C is correct because name = input('name') does two essential things: it calls input() with a prompt string (displaying name to the monitor as a cue), and it assigns the user's typed response to the variable name, which the following print(name) can then display. Why the…
Question
insert your code here
print(name)Options
- Ainput(name)
- Bname = input()
- Cname = input('name')
- Dinput('name')
How the community answered
(38 responses)- A18% (7)
- B3% (1)
- C71% (27)
- D8% (3)
Explanation
Option C is correct because name = input('name') does two essential things: it calls input() with a prompt string (displaying name to the monitor as a cue), and it assigns the user's typed response to the variable name, which the following print(name) can then display.
Why the distractors fail:
- A (
input(name)) -namehasn't been defined yet, so Python raises aNameError; it also discards whatever the user types since there's no assignment. - B (
name = input()) - correctly assigns toname, but passes no prompt string toinput(), which the exam considers incomplete usage of the function's signature (even though the precedingprintalready prompted the user). - D (
input('name')) - shows a prompt but throws away the return value with no assignment, soprint(name)would still crash with aNameError.
Memory tip: Think of input() as a vending machine - you put a message in (the prompt string), the user puts their answer in, and you must catch what comes out with =. No = means the value falls on the floor.
Community Discussion
No community discussion yet for this question.