Skip to main content

askr

Simple, validated input prompts for the terminal.

Every function keeps asking until the answer is valid, so you never have to write another while True / try / except ValueError loop around input() again.

Installation

pip install askr

Requires Python 3.10 or newer. No dependencies.

Usage

from datetime import date
from askr import ask_choice, ask_date, ask_float, ask_int, ask_password, ask_path, ask_str, ask_yn

age = ask_int("Your age: ", min_value=0, max_value=120)
price = ask_float("Price: ", min_value=0)
name = ask_str("Your name: ", min_length=1, max_length=40)

if ask_yn("Continue?"):             # Enter = yes
    ...
if ask_yn("Delete everything?", default=False):   # Enter = no
    ...

mode = ask_choice("Difficulty?", ["Easy", "Medium", "Hard"], default="Medium")

password = ask_password(confirm=True, min_length=8, require_upper=True,
                        require_digit=True, require_special=True)
pin = ask_password("PIN: ", pattern=r"\d{4}", pattern_hint="be exactly 4 digits")

file = ask_path("File to open: ", kind="file")
birthday = ask_date("Birthday: ", ("%Y-%m-%d", "%d.%m.%Y"), max_date=date.today())

What the user sees:

Your age: abc
Invalid entry. Please enter a whole number.
Your age: 200
The number must be between 0 and 120.

Difficulty?
  1. Easy
  2. Medium (default)
  3. Hard
Your choice: hard

Password:
The password must:
  - contain an uppercase letter
  - contain a special character

Functions

Function Returns Validates
ask_int int whole number, optional min_value / max_value
ask_float float finite number, optional min_value / max_value
ask_str str optional min_length / max_length, strips whitespace by default
ask_yn bool y / yes / n / no, Enter returns default
ask_choice str option number or name (case-insensitive), optional default
ask_password str hidden input, optional confirmation, length, character classes, regex
ask_path Path must exist (optional), file or folder only (optional)
ask_date date one or more formats, optional min_date / max_date

Invalid arguments from the programmer (for example min_value greater than max_value) raise a ValueError immediately instead of asking the user forever.

Ctrl+C and Ctrl+D are not caught, so your program can handle them the way it wants (KeyboardInterrupt and EOFError).

License

MIT

Release files for askr 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for askr 0.1.0
File Size Uploaded
askr-0.1.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for askr 0.1.0
File Interpreter ABI Platform
askr-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / askr-0.1.0.tar.gz

Download URL askr-0.1.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
58fca895a7e508216f39f890066fcfc10490fdbf729bd6b3b372259aa777fb72
BLAKE2b-256 checksum
How to use checksums
b2786c63a4435ed690a1f3ea0676880dd3f19c0dccd7d836c65787ad6f4933c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / askr-0.1.0-py3-none-any.whl

Download URL askr-0.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
300cda0faf631c4c32972555c76dff5964aa6b82233a52303425c8196bfb70ac
BLAKE2b-256 checksum
How to use checksums
84558debc0ba537ddc49feb4169832fabcf56941c718f8c9969c13b7467cefa9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page