2

A question from my reading of Learn Python The Hard Way:

y = raw_input("Name? ")

puts the result into variable y.

Then on line 9 in the following code, raw_input("?"), where does the result go?

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

...

3 Answers 3

3

To put it simply, it doesn't get stored - control-C (^C) gets the interpreter to stop doing what it's doing, and exits. If you type anything else at the question mark (and of course press Enter), the program will run. The raw_input is there only to wait for user input.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that "anything" won't necessarily cause the program to continue; the program will only regain control when the input is terminated with Enter.
1

The input is not stored. Here raw_input is used for the purpose of confirmation, so the value of the input is irrelevant; the program is only concerned with waiting until either Enter or Ctrlc is pressed.

Comments

1

In your case raw_input("?") represent something like Press any key to continue

In non-interactive mode _ has no special meaning.

The python interpreter understands "_" as a reference to the last value it computed, the input is stored in a special variable _

In [83]: raw_input("Enter : ")
Enter : Hi There
Out[83]: 'Hi There'

In [84]: _
Out[84]: 'Hi There'

3 Comments

+1 for mentioning _. Although it never actually gets used in the program.
That's only true when the interpreter is run interactively. It's not true in general, and if the OP adds print _ to his code and runs it, the result will be a NameError.
@DSM and the normal caveat applies using _ when it's common for gettext and co...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.