Welcome, guest | Sign In | My Account | Store | Cart

Ask/prompt the user for a short piece of data. Recipe 577098 is a much more generic/functional (but longer) take on this.

Python, 10 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def query(question, default=None):
    s = question
    if default:
        s += " [%s]" % default
    s += ": "
    answer = raw_input(s)
    answer = answer.strip()
    if not answer:
        return default
    return answer
Created by Trent Mick on Tue, 9 Mar 2010 (MIT)
Python recipes (4591)
Trent Mick's recipes (28)

Required Modules

  • (none specified)

Other Information and Tasks