How can I extend raw_input to provide user w/ default input string?

Paul Winkler slinkp23 at yahoo.com
Thu Jun 7 01:18:37 EDT 2001


Edoardo ''Dado'' Marcora wrote:
> 
> I am wondering if it is possible to provide the user with a default input
> string using raw_input or similar function.

If you just want to show the default, that could be done like this:

import sys

def raw_input_with_default(prompt, default="yes"):
    sys.stdout.write(prompt + " [%s] " % default)
    result = sys.stdin.readline().strip()
    result = result or default
    return result


So if they don't type any non-whitespace characters before hitting return,
default will be used.

If you want them to be able to *edit* the printed default string... I have no
idea.

HTH,

PW



More information about the Python-list mailing list