Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Input Functions

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 input and 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 functions.

The raw_input Function

The first way to get interactive input is the raw_input( [prompt] ) function. 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.

The raw_input( [prompt] ) function reads from a file often called stdin. When 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 Run In Terminal item in the #! menu.

Here's an example script that uses raw_input( [prompt] ).

Example 6.4. rawdemo.py

#!/usr/bin/env python
# show how raw_input works
a= raw_input( "yes?" )
print a

When we run this script from the shell prompt, it looks like the following.

MacBook-3:Examples slott$ 

python rawdemo.py

yes?

why not?

why not?
$ 

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 function.

The raw_input function's value was assigned to the variable a. The second statement printed that variable.

If we want numeric input, we must convert the resulting string to a number.

Example 6.5. stock.py

#!/usr/bin/env python
# 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

We'll chmod +x stock.py this program; then we can run it as many times as we like to get results.

MacBook-3:Examples slott$ 

./stock.py

shares: 

150

dollars: 

24

eights: 

3

value 3656.25
$ 

The 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.

MacBook-3:Examples slott$ 

./stock.py

shares: 

a bunch



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


MacBook-3:Examples slott$ 

The input Function

In addition to the raw_input function, which returns the exact string of characters, there is the input( [prompt] ) 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 eval( string ) 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 input ( p ) function is eval( raw_input( p ) ). Here's an example:

Example 6.6. inputdemo.py

#!/usr/bin/env python
shares = input('shares: ')
print shares

When we chmod +x inputdemo.py and run this program, we see the following.

$ 

./inputdemo.py

shares: 

2+1/2.0

2.5
$ 

In this case, the input function evaluated the expression 2+1/2.0.

The 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 , we'll add some sophistication that will solve some of these problems.


 
 
  Published under the terms of the Open Publication License Design by Interspire