Conversing with the user

What will we cover?
How to prompt the user to enter data and how to read that data once it is entered. We will show how to read both numerical and string based data. Also we look at how to read data input as command line arguments.

So far our programs have only dealt with static data. Data that, if need be, we can examine before the program runs and thus write the program to suit. Most programs aren't like that. Most programs expect to be driven by a user, at least to the extent of being told what file to open, edit etc. Others prompt the user for data at critical points. Let's see how that can be done before we progress any further.

>>> print raw_input("Type something: ")

As you see raw_input simply displays the given prompt and captures whatever the user types in response. Print then displays that response. We could instead assign it to a variable:

resp = raw_input("What's your name? ")
print "Hi, %s, nice to meet you" % resp 

raw_input has a cousin called input. The difference is that raw_input collects the characters the user types and presents them as a string, whereas input collects them and tries to form them into a number. For example if the user types '1','2','3' then input will read those 3 characters and convert them into the number 123.

Let's use input to decide which multiplication table to print:

multiplier = input("Which multiplier do you want? Pick a number ")
for j in range(1,13):
   print "%d x %d = %d" % (j, multiplier, j * multiplier)

INPUT

In BASIC the INPUT statement reads input from the user thus:

INPUT "What multiplier do you want, pick a number ";M
FOR J = 1 to 12
    PRINT M "x" J "= " M*J
NEXT J

As you see its very similar to Python except you put the variable at the end. Also BASIC uses INPUT for both numbers and strings. There are usually a few extra features in BASIC's INPUT statement. You should look at the documentation for your particular version.

Command Line Parameters

One other type of input is from the command line. For example when you run your text editor like:

EDIT Foo.txt

How does the editor read the filename?

In most languages the system provides an array or list of strings containing the command line words. Thus the first element will contain the command itself, the second element will be the first argument, etc. There is usually some kind of magic variable that holds the number of elements in the list.

In Python that list is held by the sys module and called argv (for 'argument values'). We can extract the elements using indexing or by iterating over the list, thus:

import sys
for item in sys.argv:
    print item
    
print "the first argument was:", sys.argv[1]

Tcl has a similar scheme with 3 variables:

  • argv0 - the command name,
  • argv - a string containing the rest of the command line and
  • argc - containing the number of words in argv

    An example of accessing the command line arguments in Tcl is:

    puts "the command was: $argv0"
    puts "The first argument was:  [lindex $argv 0]"
    

    BASIC, so far as I know, does not directly support command line arguments, although it would be possible to use operating system features to access them. That's far too advanced for this course however and I recommend that in BASIC programs you prompt the user for the values interactively.

    That's really as far as we'll go with user input in this course. It's very primitive but you can write useful programs with it. In the early days of Unix or PCs it's the only kind of interaction you got. Python, Tcl and BASIC (in its 'Visual' incarnation) are all capable of writing sophisticated GUI programs with windows, dialogs etc... but that's a bit too advanced for this course. Having said that the case study does provide a brief example of getting input via a GUI in Python but we won't be explaining too much of how it works. There are Web tutorials available for doing that once you get a good grounding in the essentials, I'll list some of them in the references page.

    Points to remember
    • Use input for reading numbers, raw_input for reading characters/strings.
    • Both input and raw_input can display a string to prompt the user.
    • BASIC's INPUT command can be used for any type of data.
    • Command line parameters can be obtained from the argv list imported from the sys module in Python, where the first item is the name of the program.
    • TCL uses the similarly named argv list to get it's command line data, but the program name is in the separate argv0
    • The __name__ variable will be set to "__main__" if the module has been run from the command line (or double clicked in Windows).

    Previous  Next  Contents


    If you have any questions or feedback on this page send me mail at: alan_gauld@xoommail.com