Python provides two simplistic built-in functions to accept input
and set the value of variables. These are not really suitable for a
complete application, but will do for our initial explorations.
Typically, interactive programs which run on a desktop use a
complete graphic user interface (GUI),
often written with the
Tkinter module or the
pyGTK module. Interactive programs which run over
the Internet use HTML forms. The primitive interactions we're showing with
raw_input are only
suitable for relatively simple programs.
Note that some IDE's buffer the program's output, making these
functions appear to misbehave. For example, if you use Komodo, you'll need
to use the "Run in a New Console" option. If you use BBEdit, you'll have
to use the "Run in Terminal" option.
You can enhance these functions somewhat by including the statement
import readline. This module silently and automatically
enhances these input functions to give the user the ability to scroll
backwards and reuse previous inputs.
You can also
import rlcompleter. This module allows you
to define sophisticated keyword auto-completion for these
The first way to get interactive input is the
This function accepts a string parameter, which is the user's prompt,
written to standard output. The next line available on standard input is
returned as the value of the function.
function reads from a file often called
running from the command-line, this will be the keyboard, and what you
type will be echoed in the command window or
Terminal window. If you try, however, to run
these examples from Textpad, you'll see that Textpad doesn't have any
place for you to type any input. In BBEdit, you'll need to use the
item in the
Here's an example script that uses
Example 6.4. rawdemo.py
# show how raw_input works
a= raw_input( "yes?" )
When we run this script from the shell prompt, it looks like the
This program begins by evaluating the
raw_input function. When
raw_input is applied to the parameter of
"yes?", it writes the prompt on standard output, and
waits for a line of input. We entered
why not?. Once
that line was complete, the input string is returned as the value of the
raw_input function's value was assigned
to the variable
a. The second statement printed that
If we want numeric input, we must convert the resulting string to
Example 6.5. stock.py
# Compute the value of a block of stock
shares = int( raw_input("shares: ") )
price = float( raw_input("dollars: ") )
price += float( raw_input("eights: ") )/8.0
print "value", shares * price
chmod +x stock.py
this program; then we
can run it as many times as we like to get results.
raw_input mechanism is very limited. If
the string returned by
raw_input is not suitable
for use by
int, an exception is raised and the
program stops running. We'll cover exception handling in detail in Chapter 17, Exceptions
Here's what it looks like.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "stock.py", line 3, in ?
shares = int( raw_input("shares: ") )
ValueError: invalid literal for int(): a bunch
In addition to the
raw_input function, which
returns the exact string of characters, there is the
) function. This
also accepts an optional parameter, which is the prompt. It writes the
prompt, then reads a line of input. It then applies the Python
) function to
evaluate the input. Typically, we expect to decode a string of digits as
an integer or floating point number. Actually, it will evaluate any
legal Python expression! Of course, illegal expressions will raise an
exception and the program will stop running.
The value of the
) function is
. Here's an example:
Example 6.6. inputdemo.py
shares = input('shares: ')
chmod +x inputdemo.py
and run this
program, we see the following.
In this case, the
input function evaluated
input function is an unreliable tool. For
yourself, the author of the program, problems with
input are easily solved. For someone else, the
vagaries of what is legal and how Python responds to things that are not
legal can be frustrating. In Chapter 17, Exceptions
add some sophistication that will solve some of these problems.