Pauli
Pauli

Reputation: 1265

raw_input() in Python

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')

...

Upvotes: 2

Views: 1094

Answers (3)

avasal
avasal

Reputation: 14854

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'

Upvotes: 1

Volatility
Volatility

Reputation: 32300

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.

Upvotes: 3

Nikita Kouevda
Nikita Kouevda

Reputation: 5746

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.

Upvotes: 1

Related Questions