How to Ask for Input in Python (input() Explained Simply)
In this article
Use the built-in input() function to ask the user something in Python: answer = input("What is your name? ") always returns a string, so convert it with int() or float() when you need a number.
Time 5 min · Steps 5 · Level Easy
The most common situation is a small script that pauses and waits for the person running it to type something, and input() does exactly that. The case where the method differs is when your question is meant for other programmers rather than the user of your program — that is a "how to ask a good question" problem, covered at the end. Everything here works in Python 3; menu labels and editor layouts can differ slightly by version.
Ask the User with input() in Five Steps
- Write the prompt inside the parentheses. Put your question as a string, e.g.
name = input("What is your name? "). The trailing space keeps the cursor off the question mark. - Store the result in a variable. Whatever the user types before pressing Enter lands in that variable as one string, including spaces.
- Convert numbers before using them.
age = int(input("How old are you? "))for whole numbers,price = float(input("Price? "))for decimals. Skip this and"5" + 5will raise a TypeError. - Reply or use the value.
print("Hello, " + name)or use the variable in a calculation such asage + 1. - Run the script, not just one line. Save the file (e.g.
greet.py) and run it from your editor's Run button or withpython greet.pyso the prompt appears and waits.
Convert the Answer Without Crashing
The one thing that trips up most beginners is that input() never gives you a number. If someone types twelve where you called int(), the program stops with a ValueError. Wrap the conversion in try/except to keep going:
try:
age = int(input("How old are you? "))
except ValueError:
print("Please type a number.")
age = 0For a question the user must answer correctly, loop until the conversion succeeds:
while True:
try:
age = int(input("How old are you? "))
break
except ValueError:
print("That is not a whole number, try again.")If you need yes/no, compare the lowercased string: if input("Continue? (y/n) ").lower().startswith("y"):. This accepts Y, yes and yep.
Match the Question to the Situation
Pick the tool by what you actually need from the user.
- If you need one typed answer at a simple prompt:
input(), always. - If the answer must be a number:
input()plusint()orfloat(), wrapped intry/except. - If the user should choose from fixed options: print numbered choices and use
if answer == "1"style checks. - If the program runs where nobody can type (some web hosts, scheduled jobs):
input()will fail, so pass the value as a command-line argument withsys.argvinstead.
| Symptom | Cause | Fix |
|---|---|---|
| TypeError: can only concatenate str to str | You added a number to the input string | Convert first: int(answer) or format with an f-string |
| ValueError: invalid literal for int() | User typed text or a decimal where you wanted a whole number | Use float() for decimals, or try/except to re-ask |
| EOFError when the script runs | No one is there to type (empty input stream) | Run the script in a terminal, or read the value from sys.argv |
| Answer has a stray space or newline | Extra typing or copy-paste | Strip it: answer.strip() |
Worked Example: A Tiny Quiz Question
Here is a complete script that asks one question, checks the answer, and handles a wrong type of input:
question = "What is 7 multiplied by 6? "
while True:
reply = input(question)
try:
number = int(reply)
break
except ValueError:
print("Please answer with a number.")
if number == 42:
print("Correct!")
else:
print("Not quite — the answer is 42.")Run it and type 42: the loop breaks on the first pass, the comparison is true, and "Correct!" prints. Type forty-two instead: int() raises ValueError, the except block prints the retry message, and the while True loop asks again. Type 41: the conversion succeeds, the comparison fails, and you get the correction message.
Ask a Question About Your Code the Right Way
If "how to ask in Python" meant asking other people for help, the same rules apply whether you post on a Q&A site, a forum, or a chat channel:
- Show the smallest complete code that fails. Ten lines beat a hundred; include the exact line that errors.
- Paste the full error message, starting from the Traceback line, not a paraphrase of it.
- Say what you expected and what happened instead, in one sentence each.
- Name your Python version (run
python --version) and any library involved, since behaviour differs between versions. - Say what you already tried, so nobody repeats it back to you.
Reading the Traceback bottom-up is a skill worth practising: the last line names the error type, and the lines above it show which function calls led there. Many "how do I fix this" questions answer themselves once you read that last line carefully — a TypeError naming str and int is almost always the input-conversion issue from the table above.
For the function details themselves, the official Python documentation is the page to check: it lists input()'s exact behaviour, including the EOFError case, and is kept current with each Python release.
Official source: Python Software Foundation