[Tutor] Newbie question re: user interaction
Thu, 1 Feb 2001 15:55:40 -1000
[Gus Hungerford asked]
> Anyway, what I'm writing to ask about is, how do I do the
> equivalent of the following BASIC function in Python?
> 10 INPUT "What is your name?" N$
> 20 PRINT "Hello, " N$ "!"
Look at the raw_input function. The reference example (below) is almost
what you need. You'll probably want this:
answer = raw_input('What is your name? ')
>From the 'Python Library Reference':
If the prompt argument is present, it is written to standard output without
a trailing newline. The function then reads a line from input, converts it
to a string (stripping a trailing newline), and returns that. When EOF is
read, EOFError is raised. Example:
>>> s = raw_input('--> ')
--> Monty Python's Flying Circus
"Monty Python's Flying Circus"
If the readline module was loaded, then raw_input() will use it to provide
elaborate line editing and history features.