Skip to main content

Simple text prompts and validation for user input

Project description


Interactive command-line programs need to query users for information, be it text, choices from a list, or simple yes-or-no answers. qanda is a Python module of simple functions to prompt users for such information, allowing validation and cleanup of answers, default responses, consistent formatting and presentation of help text, hints and choices. It is not a replacement for textual interfaces like curses and urwid, but intended solely for simple console scripts with user input is required.


The simplest way to install qanda is via easy_install or an equivalent program:

% easy_install qanda

Alternatively the tarball can be downloaded, unpacked and run:

% tar zxvf qanda.tgz
% cd qanda
% python install

qanda has no requisites and should work with just about any version of Python.

Using qanda


>>> from qanda import prompt
>>> prompt.string ("What is your name")
What is your name: Foo
>>> fname = prompt.string ("Your friends name is",
                help="I need to know your friends name as well before I talk to you.",
                hints="first name",

I need to know your friends name as well before I talk to you.
Your friends name is (first name) [Bar]:
>>> print fname
>>> years = prompt.integer ("And what is your age", min=1, max=100)
And what is your age: 101
A problem: 101 is higher than 100. Try again ...
And what is your age: 28

Central concepts

qanda packages all question-asking methods in a Session class. This allows the appearance and functioning of all these methods to be handled consistently and modified centrally. However, you don’t necessarily have to create a Session to use it - there’s pre-existing Session in the variable called prompt:

>>> from qanda import Session
>>> s = Session()
>>> from qanda import prompt
>>> type (prompt)
<class 'qanda.session.Session'>

The question methods are named after the type of data they elicit:

>>> print type(prompt.integer ("Pick a number"))
Pick a number: 2
<type 'int'>
>>> print type(prompt.string ("Pick a name"))
Pick a name: Bob
<type 'string'>

Many of the question methods with accept a list of “converters”, each of which is used to sucessively transform or validate user input. If input fails validation, the question is posed again. qanda supplies a number of basic validators:

ToInt, ToFloat

Convert inputs to other types


nly allow values that match a certain pattern


Check that input falls within given bounds


Check that input length falls within given bounds


Map values to other values


Ensure values fall within a fixed set



qanda home page


qanda on PyPi


v0.1dev (20110624)

  • Initial release, sure to be buggy and incomplete

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qanda-0.1dev.tar.gz (61.9 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page