Prompts / Coding & Software Engineering

ChatGPT prompt for Python user input

This prompt produces working Python code that collects, validates, and uses user input. Use it when a script needs reliable console input instead of assuming users enter perfect data.

PromptOpen ChatGPTOpen Claude
Help me implement user input in a Python program.

Program goal: [PROGRAM GOAL]
Python version and environment: [PYTHON VERSION AND ENVIRONMENT]
Inputs to collect: [INPUT REQUIREMENTS]
What should happen after valid input: [NEXT ACTION]
Existing code or error, if any: [EXISTING CODE OR ERROR]

Write a complete, runnable solution first, then explain it in plain English. Use input() only for terminal interaction unless I explicitly ask for a GUI or web form. For every input, state the accepted format, convert types deliberately, and validate before the value is used. Treat blank input, whitespace-only input, non-numeric text where a number is required, invalid menu choices, out-of-range values, and Ctrl+C/EOF appropriately for this program.

Use a retry loop with a helpful error message when retrying makes sense. Do not use bare except clauses or silently replace invalid values with guesses. Preserve meaningful leading zeros for identifiers such as ZIP codes or account IDs. If a default is appropriate, show it in the prompt and require the user to press Enter to accept it.

Return these sections:
1. Complete code in one code block.
2. A short sample terminal session showing valid and invalid input.
3. An explanation of validation, conversion, and exit behavior.
4. Tests or manual cases I should try.

Before answering, check that every variable is defined before use and that each conversion is protected by validation or a specific exception handler. Flag any ambiguity in my requirements instead of inventing business rules. Ask up to 3 clarifying questions only if a required input is missing.

Fill in

PlaceholderWhat to enterExample
[PROGRAM GOAL]Describe what the Python program is meant to do.Ask a volunteer for their name, age, and available shift, then print their registration summary.
[PYTHON VERSION AND ENVIRONMENT]State the Python version and where the code will run, such as a terminal or Jupyter notebook.Python 3.12 in a macOS terminal.
[INPUT REQUIREMENTS]List each value to collect, its expected format, valid range, and whether it is optional.Name: 2-50 nonblank characters; age: whole number 16-90; shift: morning, afternoon, or evening; Enter on shift accepts afternoon.
[NEXT ACTION]Explain how the program should use the validated input.Print a confirmation and store the values in a dictionary for a later CSV export.
[EXISTING CODE OR ERROR]Paste relevant code or the exact error message, or write None.None; start from scratch.

How to use

  1. Replace each bracketed input with the program’s actual input rules.
  2. Paste the full prompt into ChatGPT, Claude, or Gemini.
  3. Run the sample code and try every invalid case listed in the answer.
  4. Follow up with: "Adapt this for input from a CSV file instead of the terminal, keeping the same validation rules."

Variations

Reusable input function

Use this when several parts of a script need the same validated input pattern.

Variation
Create a reusable Python function for this input type: [INPUT TYPE]. It must accept a prompt, validate these rules: [VALIDATION RULES], return the correctly typed value, and allow [RETRY OR EXIT BEHAVIOR]. Show a small runnable example that calls it twice. Explain why the function catches only the exceptions it expects. Do not use a bare except or silently coerce invalid data. Before answering, verify that the function cannot return an invalid value.

Password input

Use this for terminal credentials or secrets that must not appear on screen.

Variation
Write Python 3 code that securely asks for [SECRET TYPE] in a terminal and then [PERMITTED ACTION]. Use getpass so typed characters are not echoed. Specify how to handle blank values, cancellation, and confirmation mismatch if confirmation is needed. Do not log, print, hard-code, or include the secret in an exception message. Return runnable code, a brief explanation, and manual test cases. Flag any requested behavior that would expose the secret.

Interactive menu

Use this when users must select an action from a fixed command-line menu.

Variation
Build a Python terminal menu for [PROGRAM PURPOSE] with these choices: [MENU OPTIONS]. Keep showing the menu until the user chooses [EXIT OPTION]. Accept only the listed choices, handle uppercase or surrounding spaces if appropriate, and give a useful message for invalid entries. Each valid choice should run this behavior: [CHOICE BEHAVIOR]. Return complete runnable code plus a sample session. Before answering, verify that every menu branch is reachable and the exit path ends cleanly.

Tips

  • Write input rules as data types and ranges, not as loose labels such as “enter your age.”
  • Keep identifiers as strings when formatting matters; converting a phone number or ZIP code to int can destroy meaningful zeros.
  • For production scripts, decide explicitly whether malformed input should retry, skip a record, or terminate with a nonzero exit status.
  • Avoid mixing input collection with business logic; a small validation function is easier to test than repeated inline loops.

FAQ

Should Python input validation use try/except?

Use a specific exception handler around conversions that can fail, such as int(). Combine it with range and format checks after conversion.

How do I let a user leave a field blank?

Strip the input, check for an empty string first, then either apply a stated default or reject it with a clear message.

Can AI write input code for a GUI?

Yes, but specify the toolkit, such as Tkinter or PySide, because GUI validation and error messages differ from terminal input.

Related prompts

All Coding & Software Engineering prompts →

Get a few prompts like this every week

Something Big is a free AI newsletter read by 50,000+ professionals. About three short emails a week with prompts and tools that work, plus what changed in AI and what to do about it.